**I am trying to read the records from and to a CSV file in C. Of course i want to stop when I have read all the records. If I use a for loop it works perfect except I have to know how many records there are. if I change to a while loop it only reads one record. I read many of your great examples and tried different things. I have tried !=EOF and == all number from 1 to 10. It still only reads one line. Can you give me a suggestion please. (please don't tell me to give up C programing altogether)
This code works and reads/writes the data just fine:
**

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
  FILE *in_file = fopen("c:\\cstuff\\texttext.csv", "r");
  FILE *out_file = fopen("c:\\cstuff\\outtext.csv", "a");
  int z =0;
  char one[30];
  char two[30];
  char three[30];
for (z = 1; z < 15; z++)
/*while(fscanf(in_file,"%[^,]",one)=2);*/
{
fscanf(in_file,"%[^,]",one);
fscanf(in_file,"%*c %[^,]",two);
fscanf(in_file,"%*c %s\n",three);
/*z++; /*Increment z for line numbers*/
/*fscanf(in_file,"%[^,] %*c %[^,] %*c %s\n",one, two, three);*/
/* put the data back in another file with the line number*/
fprintf(out_file,"%i,",z);        /* put the count first followed by a comma*/
fprintf(out_file,"%s,",one);      /*put the first variable followed by a comma*/
fprintf(out_file,"%s,",two);      /*put the second variable followed by a comma*/
fprintf(out_file,"%s\n",three);   /*put the third variable position to next line*/
}                                 /*end while/for  loop */
/* close the files */
fclose(in_file);
fclose(out_file);
return 0;
}

if I comment out the for loop and put in a while loop it will only read one record.
This is the code that only reads one record:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
  FILE *in_file = fopen("c:\\cstuff\\texttext.csv", "r");
  FILE *out_file = fopen("c:\\cstuff\\outtext.csv", "a");
  int z =0;
  char one[30];
  char two[30];
  char three[30];
/*for (z = 1; z < 15; z++)*/
while(fscanf(in_file,"%[^,]",one)==1);
{
/*fscanf(in_file,"%[^,]",one);*/
fscanf(in_file,"%*c %[^,]",two);
fscanf(in_file,"%*c %s\n",three);
z++; /*Increment z for line numbers*/
/*fscanf(in_file,"%[^,] %*c %[^,] %*c %s\n",one, two, three);*/
/* put the data back in another file with the line number*/
fprintf(out_file,"%i,",z);        /* put the count first followed by a comma*/
fprintf(out_file,"%s,",one);      /*put the first variable followed by a comma*/
fprintf(out_file,"%s,",two);      /*put the second variable followed by a comma*/
fprintf(out_file,"%s\n",three);   /*put the third variable position to next line*/
}                                 /*end while/for  loop */
/* close the files */
fclose(in_file);
fclose(out_file);
return 0;
}

the data looks like this:
first,bfirst,cfirst
second,bsecond,csecond
third,bthird,cthird
fourth,bfourth,cfourth
fifth,bfifth,cfifth
sixth,bsixth,csixth
seventh,bseventh,cseventh
eighth,beighth,ceighth
ninth,bninth,cninth
tenth,btenth,ctenth
eleventh,beleventh,celeventh
twelveth,btwelveth,ctwelveth
thirteenth,bthirteenth,cthirteenth
fourteenth,bfourteenth,cfourteenth

How embarrassing! After posting and rereading my code I see the ; after my while statement. I removed it and the code with the while statement works fine. Thanks anyway for all the input that I read before I posted.

:-) Yeah, I've been bitten by that one a few times in the past 30+ years! Good find, and do not be shy to post here again when you have a "real" problem! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.