0

I am very new to programming so please excuse this stupid question. I want to access CSV files in excel, but I'm not sure how to count the number of rows in the file. Should I use feof() and how?

*FILE *data;
int count;
data = fopen("test.csv", "r");
while( feof(data) == 0 )
{
    count++;
}
printf("%d", count);*
  • Hint: how should you determine the end of a row? Otherwise, show us what you've tried. – plinth Oct 06 '14 at 19:29
  • C's `feof()` function determines whether the specified stream is positioned at the end of the file. Therefore, you might consider reading the CSV line by line, and using `feof()` to determine when you have read the last line. Why don't you see what you can work out? – John Bollinger Oct 06 '14 at 19:29
  • When i build there's no problem, but when I run the program it seems to run forever as if the statement is always true. – Adriaan Sadie Oct 06 '14 at 19:39
  • `while (feof() == 0` is wrong. `feof` only gets triggered after some actual input function *fails*. If you don't need to distinguish `feof` from `ferror`, use `while (fgets(...) != 0)`. – o11c Oct 06 '14 at 19:39
  • @o11c: More clearly: `while (fgets(...) != NULL)` – Keith Thompson Oct 06 '14 at 19:48
  • Awesome! i used that format and it works now, thanx a lot guys!! – Adriaan Sadie Oct 06 '14 at 20:02

0 Answers0