Using fgets and sscanf Line into Strings 
Author Message
 Using fgets and sscanf Line into Strings

I am having trouble reading in character strings that do not contain
blanks, newlines or tabs.

Each string is to be no more than 4 characters.

The input will be in the form of:
eg.

12 3 +8ha -98

I have tried:

struct numRec {
        char strNum[4];

Quote:
}nums[10];

int main()
{

        char lineArary[30];

fgets(lineArray , sizeof(lineArray), stdin);
sscanf(lineArray, "%s%s%s%s%s", nums[0].strNum,
nums[1].strNum..nums[4].strNum);

Quote:
}

This works, but I do not no how many strings will be entered!

So I have tried using a loop until end of line is encountered.
eg.

int i=0;

while(nums[i].strNums[0] != '\n') {
        sscanf(lineArray, "%s", nums[i].strNum);
        i++;

Quote:
}

What can I do?????? How should it detect the end of the line?

Thanks for any help. Sam Gundry.



Sat, 05 Feb 2005 09:56:53 GMT  
 Using fgets and sscanf Line into Strings

Quote:

>I am having trouble reading in character strings that do not contain
>blanks, newlines or tabs.

>Each string is to be no more than 4 characters.

>The input will be in the form of:
>eg.

>12 3 +8ha -98

>I have tried:

>struct numRec {
>    char strNum[4];

This is inadequate for holding a string of length 4 (such as +8ha).
You need room for the terminating '\0'.

Quote:
>}nums[10];

>int main()
>{

>    char lineArary[30];

>fgets(lineArray , sizeof(lineArray), stdin);
>sscanf(lineArray, "%s%s%s%s%s", nums[0].strNum,
>nums[1].strNum..nums[4].strNum);

>}

>This works, but I do not no how many strings will be entered!

You could replace the hardcoded five-string sscanf with a loop driven
by strtok.  Once strtok has found the start of a token and placed a
'\0' at its end, you could simply strcpy to nums[i].strNum and then
increment i.  You terminate the loop when strtok says there are no
more tokens.

Quote:

>So I have tried using a loop until end of line is encountered.
>eg.

>int i=0;

>while(nums[i].strNums[0] != '\n') {
>    sscanf(lineArray, "%s", nums[i].strNum);
>    i++;
>}

Unfortunately, this will extract the same string (the first one) each
time.

Quote:

>What can I do?????? How should it detect the end of the line?

The end of line can be detected by the '\0' that fgets guarantees to
place in your buffer.  If the buffer is not full, it will also place
the '\n' just before this '\0'.

<<Remove the del for email>>



Sat, 05 Feb 2005 11:34:30 GMT  
 Using fgets and sscanf Line into Strings

Quote:
> I am having trouble reading in character strings that do not contain
> blanks, newlines or tabs.

> Each string is to be no more than 4 characters.

You have to ask yourself, how do you separate each "string"?  That is,
if they aren't whitespace delimited, how are they distinguished?

const char *frobaz = "I'm:A:Itty:Dog";

In the above, we can delimit on ':', but what you've told us is
insufficient.

Quote:
> The input will be in the form of:
> eg.

> 12 3 +8ha -98

Well, those have exactly one space separating them.  So, does the
input have space delimiters or not?

Quote:
> I have tried:

> struct numRec {
>    char strNum[4];
> }nums[10];

> int main()
> {

>    char lineArary[30];

> fgets(lineArray , sizeof(lineArray), stdin);

Check fgets() return value, or else!

Quote:
> sscanf(lineArray, "%s%s%s%s%s", nums[0].strNum,
> nums[1].strNum..nums[4].strNum);

The .. operator is not recognized in ISO C... ;-)

That sscanf call says, "parse out up to five whitespace
delimited strings of indeterminate length.  Trust me,
I've allocated enough space."  You might be interested
in "%4s".  Also, check the return value; it gives you
important information. Note: If your tokens can have
up to four characters, you'll want five character
strings (for terminating '\0').

Quote:
> This works, but I do not no how many strings will be entered!

> So I have tried using a loop until end of line is encountered.
> eg.

> int i=0;

> while(nums[i].strNums[0] != '\n') {
>    sscanf(lineArray, "%s", nums[i].strNum);
>    i++;
> }

Ahh, the loop that never ends...

sscanf (lineArray, ...) always starts at the beginning of
the string (unless you take appropriate action).
You are probably better off ditching the scanf() family
when you don't have predictable input.  Look into strpbrk
or strtok.  You'll might need malloc/realloc here too --
depending on whether you want/need to process the entire
line at one go, or if you can do it in pieces.



Sat, 05 Feb 2005 12:06:42 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Parsing a comma delimited line with optional fields using sscanf or strtok

2. using fgets() -- dynamically allocating string size

3. How to read a string using FGETS function.

4. HELP! Problems string->float using ATOF,SSCANF

5. fgets/sscanf

6. fgets & sscanf problem

7. fgets/sscanf

8. fgets & sscanf help

9. fgets and sscanf not working as expected

10. sscanf & fgets VS. scanf

11. fgets() sscanf() EOF confusion

12. whole line with fgets

 

 
Powered by phpBB® Forum Software