![]() |
|
sscanf and multiple strings |
KaBlammyman
Member #455
June 2000
![]() |
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. 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 when printed out, it looks like this: Quote:
channel = 1 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
Member #3,653
June 2003
![]() |
%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
Free Market Evangelist
September 2000
![]() |
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
Member #455
June 2000
![]() |
thanx guys. I already made a work around, but I will try to use the code you gave Bob.
|
BAF
Member #2,981
December 2002
![]() |
Why not use PHP or something made for the job. It will likely be TONS easier. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Or use C++, thats also a good amount easier. |
|