Open, read and write csv

0

I have a C code that is using the base structure for reading.

Read

FILE *input;
input=fopen(argv[1],"r");
points=(Point*)malloc(sizeof(Point)*num_points);
readPoints(input,points,num_points);
fclose(input);

Save

FILE* output=fopen(argv[2],"w");
fprintf(output,"%d\n",num_clusters);
fprintf(output,"%d\n",num_points);
fclose(output);

My question is how can I read a csv file so that I do not know how many lines it has, and then write the data to another csv file. Would it be the same C procedure with EOF /! EOF and the code the way I did, or will it be done differently?

Thank you very much.

    
asked by anonymous 13.07.2017 / 21:44

1 answer

0

The function int feof(FILE *) , declared in stdio.h , returns 0 if the file still has bytes to read, and 1 case has found the end of the file. With this, you can execute a loop and transcribe the CSV:

FILE * input, * output;
char buffer[2048];
input = fopen(argv[1], "r");
output = fopen(argv[2], "w");
while (! feof(input)) {
    fgets(buffer, sizeof(buffer), input);
    tratar(buffer); /* esta função vai executar algum tratamento arbitrário na linha */
    fputs(buffer, output);
}
fclose(input);
fclose(output);
    
14.07.2017 / 18:58