I want to open a file and count the number of strings in the first line.
Assuming that each string is delimited by spaces.
I tried to do some code but yet not successful, can anyone help?
int main(int argc, char* argv[])
{
int count = 0;
FILE *stream;
char str[10];
//open a file
if( (stream = fopen( "input.txt", "r" )) == NULL )
printf( "The file 'input.txt' was not opened\n" );
else
printf( "The file 'input.txt' was opened\n" );
//read the first line
while( fscanf(stream, str) != '\n')
{
count++; //count the number of strings in the line (strings delimited by space)
cout << "\ncount: " << count;
}
return 0;
}