#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;
}
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
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...
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.