Question on sscanf

D

Dan Smith

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
 
J

Jirka Klaue

Dan said:
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
 
M

Martin Ambuhl

Dan said:
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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top