sscanf and multiple strings
KaBlammyman

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.

Jakub Wasilewski

%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).

Bob

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.

KaBlammyman

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

BAF

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

ImLeftFooted

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

Thread #590278. Printed from Allegro.cc