what is the best way to determine how many values are contained in an input pointer file and store in a variable? any ideas appreciated.
what is the best way to determine how many values are contained in an input pointer file and store in a variable? any ideas appreciated.
Open the file.
Read its contents.
Store the restults.
Close the file.
Other than that, you'll have to clarify what you mean by "an input pointer file" and "store in a variable".
Quzah.
Hope is the first step on the road to disappointment.
sorry, i was a little vague, but actually im just looking for a function to read the # of data values in the file.
like if file contained: 10,20,30,40,50,60
the function would read 6 values... and store 6 in a variable
i can use fscanf to read data but not to tell how many values are present.
Some variant of this, perhaps?Code:/* file.txt 10,20,30,40,50,60 */ #include <stdio.h> int main(void) { const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file ) { int value, count = 0; while ( fscanf(file, "%d%*c", &value) == 1 ) { ++count; } printf("count = %d\n", count); fclose(file); } return 0; } /* my output count = 6 */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*