Copy from txt file to the other

6
int main(void) {
    void copiaConteudo(FILE *arquivo, FILE *arquivo1);
    FILE *arquivo = fopen("tmp/exercicio.txt","r");
    if (arquivo == NULL)
    {
        printf ("Não foi possível abrir o arquivo");
        return 1;
    }

    FILE *arquivo1 = fopen("home/novo.txt","w");    
    copiaConteudo(arquivo,arquivo1);        
    fclose(arquivo);
    fclose(arquivo1);
    return 0;
}

void copiaConteudo(FILE *arquivo, FILE *arquivo1)
{
    string teste;
    teste = "Teste";
    char ler[100];
    while(fgets(ler,100,arquivo) != NULL)

    if (ler.find("Teste"))
    {
        fputs("0/",arquivo);    
    }
    else
    {
        fputs(ler, arquivo1);
    }
}

I have this code, that my intention is to copy from the file exercicio.txt to the file new.txt, I can copy everything, but now I wanted to include a parameter so it does not copy the lines containing "test." < br> My if there in the end is my attempt that did not work, how can I do this?

    
asked by anonymous 31.08.2017 / 21:50

4 answers

4

Here is a (tested) solution in language C able to perform a buffered binary copy of two files:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>


int main( int argc, char * argv[] )
{
    FILE * src = NULL;
    FILE * dst = NULL;
    size_t n = 0;
    size_t m = 0;
    unsigned char buf[ 1024 * 8 ];


    /* Verifica sintaxe */
    if( argc != 3 )
    {
        printf("Erro de sintaxe: %s ARQUIVO_ORIGEM ARQUIVO_DESTINO\n", argv[0] );
        return EXIT_FAILURE;
    }

    /* Abre arquivo de origem para leitura  */
    src = fopen( argv[1], "rb" );

    if(!src)
    {
        printf("Erro abrindo arquivo de origem para leitura: %s [%s]\n", argv[1], strerror(errno) );
        return EXIT_FAILURE;
    }


    /* Abre arquivo de destino para gravacao */
    dst = fopen( argv[2], "wb" );

    if(!dst)
    {
        fclose(src);
        printf("Erro abrindo arquivo de destino para escrita: %s [%s]\n", argv[2], strerror(errno) );
        return EXIT_FAILURE;
    }

    /* Efetua a copia dos arquivos */
    do {
        m = 0;

        n = fread( buf, 1, sizeof(buf), src );

        if( n > 0 )
            m = fwrite( buf, 1, n, dst );

    } while( (n > 0) && (n == m) );

    /* Verifica se houve erro durante a copia */
    if( m )
    {
        printf("Erro copiando arquivo: %s\n", strerror(errno) );

        fclose(dst);
        fclose(src);

        remove(argv[2]); /* Remove arquivo parcialmente copiado */

        return EXIT_FAILURE;
    }

    /* Finaliza com sucesso */

    fclose(dst);
    fclose(src);

    return EXIT_SUCCESS;
}
    
31.08.2017 / 22:27
4

Well, since you've tagged the question with C++ and it actually seems like you're using C++ for some high-level functions ( string::find() for example), I'll give you an answer using only C++ / p>

Commenting on the string :: find function, if it finds the requested value , it returns the position where it starts at string , otherwise it returns the value std::string::npos

Below is a functional program which eliminates any line that has a 'test' word

#include <fstream>
#include <string>

int main()
{
    auto input = std::ifstream("exercicio.txt");
    auto output = std::ofstream("novo.txt");

    std::string msg;
    while (std::getline(input, msg))
    {
        if ( msg.find("teste") != std::string::npos )
            continue;
        output << msg << std::endl;
    }
}
    
01.09.2017 / 06:01
2

If it is valid to use a command line (such as linux, mac, even on windows) the concept of filter might be interesting. A filter accepts a textual input and returns a textual output - which allows threads, and can be used as to various input files.

Via the command line we can use filters created by us or existing in the operating system.

Hypothesis 1: pre-defined grep filter (linux, mac or windows with gnu utilities installed)

 grep -v 'teste' < exercicio.txt > novo.txt

grep -v - lets only pass lines that do not contain the regular expression matches the first parameter

Hypothesis 2: user-created filter (based on the solution @Amadeus (+1))

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[]){
   string msg;
   if (argc == 2 ){
      while (getline(cin,msg)) {
         if ( msg.find(argv[1]) == string::npos )
            cout << msg << endl;
      }
   }
}

This filter is receiving the rejection pattern by argument. Usage:

g++ -o filtro filtro.cpp
filtro teste < exercicio.txt > novo.txt
    
01.09.2017 / 11:32
1

I could not quite understand std :: find, but I'll use your examples to train, thanks for the answers!

I ended up solving it this way:

int main(void) {
  void copiaConteudo(FILE *arquivo, FILE *arquivo1);
  FILE *arquivo = fopen("tmp/exercicio.txt","r");
if (arquivo == NULL)
{
    printf ("Não foi possível abrir o arquivo");
    return 1;
}

FILE *arquivo1 = fopen("home/novo.txt","w");

copiaConteudo(arquivo,arquivo1);

fclose(arquivo);
fclose(arquivo1);
return 0;
}

void copiaConteudo(FILE *arquivo, FILE *arquivo1)
{

  char ler[100];
  char lu[20];
  while(fgets(ler,100,arquivo) != NULL)
{
        if (strcmp(ler,"Teste") < 0)
        {
            fputs(ler, arquivo1);
        }
}
    
01.09.2017 / 13:53