In c++ how do I count the number of strings for the first line of a stream?

When you read a string, increment a counter.

When you read a string, increment a counter.

Is it something like this?

int main(void)
{
	static const char filename[] = "input.txt";
	FILE *file = fopen(filename, "r");
	if ( file != NULL )
	{
		char name[20];
		int cnt=0;

		while (fscanf(file, &name) != "\n")
		{
			cnt++;
		}
	
		cout << cnt;			

		fclose(file);

	}

	return 0;
}

I'm having problems doing it...

Maybe tinker with this:
http://www.daniweb.com/code/snippet444.html

[edit]Or maybe this instead.

#include <stdio.h>

int main(void)
{
   static const char filename[] = "file.txt"; /* the name of a file to open */
   FILE *file = fopen(filename, "r"); /* try to open the file */
   if ( file != NULL )
   {
      char line[BUFSIZ]; /* space to read a line into */
      while ( fgets(line, sizeof line, file) != NULL )
      {
         if ( line[0] != '#' && line[0] != '\n' )
         {
            const char *ptr = line;
            char field [ 32 ];
            int n, count = 0;
            fputs(line, stdout);
            while ( sscanf(ptr, "%31s%n", field, &n) == 1 )
            {
               printf("%d : \"%s\"\n", count, field);
               ptr += n + 1;
               ++count;
            }
         }
      }
   }
   return 0;
}

(Assuming this is still related to the [thread=47259]configuration file[/thread] stuff.)

Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...

Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...

Those are called words, not strings. A string is just a colection of one or more characters of any length, such as

"Hello World This Is a String".

When learning to write C or C++ programs, it is important to also learn the correct terminology to avoid ambiguity.

Sorry to be boring but I want to count the number of strings only in the first line.
I mean a string each group of one or more caracters limited by a space...

Can you help me? I tried to use the code you indicated but without sucess...

With this as the "file.txt":

#
#	The number of inputs for each epoch
#	Default value if omitted: 60
#
InputNumber	60


#
#	The maximum epochs allowed in the network to converge
#	Default value if omitted: 300
#
MaxEpochs	300


#
#	The number of neurons in the input layer
#	Default value if omitted: 19
#
InputNeurons	19


#
#	The number of neurons in the hidden layer
#	Default value if omitted: 22
#
HiddenNeurons	22


#
#	The number of neurons in the output layer
#	Default value if omitted: 1
#
OutputNeurons	1


#
#	Default value if omitted: 0.01
#
LearningRate	0.01


#
#	The dimension of each input
#	Default value if omitted: 48
#
InputDim	48

The code I posted ought to produce this.

InputNumber	60
0 : "InputNumber"
1 : "60"
MaxEpochs	300
0 : "MaxEpochs"
1 : "300"
InputNeurons	19
0 : "InputNeurons"
1 : "19"
HiddenNeurons	22
0 : "HiddenNeurons"
1 : "22"
OutputNeurons	1
0 : "OutputNeurons"
1 : "1"
LearningRate	0.01
0 : "LearningRate"
1 : "0.01"
InputDim	48
0 : "InputDim"
1 : "48"
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.