Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » sscanf and multiple strings

This thread is locked; no one can reply to it. rss feed Print
sscanf and multiple strings
KaBlammyman
Member #455
June 2000
avatar

Hello.

I am writing a CGI script in C, and I need to use sscanf to get the data that the user inputs in 4 different fields. The first field (or input box) is for an integer called "channel", and the other 3 are all strings called "event","time", and "date".

To get the data, I do the following.
data = getenv("QUERY_STRING");// gives me this: channel=1&event=someEvent&time=1%3A00&date=2%2F3%2F2007

Here is the problem, the integer to hold the "channel" value gets the correct value, yet, instead of only the event string going into event, the date string going into date, etc, the WHOLE string goes into event! So, the user will input this:

Quote:

Channel 1
Event someEvent
Time 1:00
Date 2/25/2007

when printed out, it looks like this:

Quote:

channel = 1
event = someEvent&time=1%3A00&date=2%2F3%2F2007

do you see the problem? How do I use sscanf to get more than one string? I cant use a delimiter since I wont know how much input to expect.

Here is a snippet of what I have:

int a; 
char b[50], c[50], d[50];

printf("%s%c%c\n", "Content-Type:text/html;charset=iso-8859-1",13,10); 
printf("\n"); 
printf("<p>Scheduler\n"); 
    
data = getenv("QUERY_STRING"); 

 if(data == NULL) 
       printf("Error! Error in passing data from form to script.");

sscanf(data,"channel=%d&event=%s&date=%s&time=%s",&a,b,c,d);

Any help is greatly appreciated.

->Insert clever quote here<-
http:my website

Jakub Wasilewski
Member #3,653
June 2003
avatar

%s reads until it encounters any whitespace, so you're pretty much out of luck with *scanf.

Just write your own little function for splitting the contents, shouldn't be that much work. Or, you could use a regular expression, but in C that's probably overkill for a single simple script (you'd have to link an additional library).

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Bob
Free Market Evangelist
September 2000
avatar

Try this instead:

sscanf(data, "channel=%d&event=%[^&]&date=%[^&]&time=%[^&]",&a,b,c,d);

Although, you may want to tokenize the string based on '&' first, so that you can parse the arguments in any order instead of the explicit order you currently have.

--
- Bob
[ -- All my signature links are 404 -- ]

KaBlammyman
Member #455
June 2000
avatar

thanx guys. I already made a work around, but I will try to use the code you gave Bob. 8-)

->Insert clever quote here<-
http:my website

BAF
Member #2,981
December 2002
avatar

Why not use PHP or something made for the job. It will likely be TONS easier.

ImLeftFooted
Member #3,935
October 2003
avatar

Or use C++, thats also a good amount easier.

Go to: