Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question on sscanf

Reply
Thread Tools

Question on sscanf

 
 
Dan Smith
Guest
Posts: n/a
 
      08-14-2003
Ok, I'm stumped. Either sscanf is broken or I'm not understanding
somthing. Probably the later. Hopefully someone here can help me out
with this before I chew my own leg off.

First what I'm trying to do:

I have a line in a text file that looks like

'TYPEBE','P6','FFGHT'

and I'm trying to get the data in the between the first two quote marks
into to strings for use later in the program.


The code I'm using is:

char line[600];
char string1[7];
char string2[7];

.....

fgets (line,256,xiresults_file);
sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);

.....

This code sets string1 to TYPEBE, but string 2 is not being set to P6
as I expected it to. I'm probably doing something wrong, but I
just can't figure out what. Thanks in advance for the help

--
Dan Smith

"You don't have to be crazy to work here, we have on the job training"



 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      08-14-2003
Dan Smith wrote:
> Ok, I'm stumped. Either sscanf is broken or I'm not understanding
> somthing. Probably the later. Hopefully someone here can help me out
> with this before I chew my own leg off.
>
> First what I'm trying to do:
>
> I have a line in a text file that looks like
>
> 'TYPEBE','P6','FFGHT'
>
> and I'm trying to get the data in the between the first two quote marks
> into to strings for use later in the program.
>
> The code I'm using is:
>
> char line[600];
> char string1[7];
> char string2[7];
>
> ....
>
> fgets (line,256,xiresults_file);
> sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);

^ ^^
%[] and %s are distinct conversion specifiers.

sscanf(s, "%*c%[^']%*c%*c%*c%[^']s", string1, string2);
or
sscanf(s, "%*c%[^']%*3s%[^']s", string1, string2);

Jirka

 
Reply With Quote
 
 
 
 
Jirka Klaue
Guest
Posts: n/a
 
      08-14-2003
I wrote:
....
> sscanf(s, "%*c%[^']%*c%*c%*c%[^']s", string1, string2);

^
> sscanf(s, "%*c%[^']%*3s%[^']s", string1, string2);

^
Forgot do delete these s', too.

Jirka

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-14-2003
Dan Smith wrote:
> Ok, I'm stumped. Either sscanf is broken or I'm not understanding
> somthing. Probably the later. Hopefully someone here can help me out
> with this before I chew my own leg off.
>
> First what I'm trying to do:
>
> I have a line in a text file that looks like
>
> 'TYPEBE','P6','FFGHT'
>
> and I'm trying to get the data in the between the first two quote marks
> into to strings for use later in the program.
>
>
> The code I'm using is:
>
> char line[600];
> char string1[7];
> char string2[7];
>
> ....
>
> fgets (line,256,xiresults_file);
> sscanf (line,"%*c%[^']s%*c%*c%*c%[^']s",string1,string2);
>
> ....
>
> This code sets string1 to TYPEBE, but string 2 is not being set to P6
> as I expected it to. I'm probably doing something wrong, but I
> just can't figure out what.


Mainly, it's that you have extraneous literal 's's in your format string.

#include <stdio.h>


int main(void)
{
char line[] = "'TYPEBE','P6','FFGHT'";
char String1[7];
char String2[7];
sscanf(line, "'%[^']','%[^']", String1, String2);
printf("Input line: \"%s\"\n", line);
printf("Substrings: \"%s\" and \"%s\"\n", String1, String2);
return 0;
}


Input line: "'TYPEBE','P6','FFGHT'"
Substrings: "TYPEBE" and "P6"


--
Martin Ambuhl

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sscanf Question alij C Programming 6 06-02-2007 09:42 PM
sscanf question sieg1974@yahoo.com C Programming 5 11-23-2006 07:39 PM
Programmer wannabee question about sscanf broeisi C Programming 10 03-05-2006 05:16 AM
sscanf() question? jchludzinski@gmail.com C Programming 5 06-09-2005 05:45 PM
sscanf fixed-width integer question Brent Lievers C Programming 8 04-26-2004 05:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57