LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   scanf question (https://www.linuxquestions.org/questions/programming-9/scanf-question-274984/)

jnusa 01-07-2005 07:55 AM

scanf question
 
How would you go about to discard or overlook a section of a string, when using scanf?

I'm trying to scan a http headerline. I need to read the status code

e-g-
Code:

int status;
status atoi(scanf(hdrstr, "HTTP/1.%* %s));

will %* discard whatever the char is?

rjlee 01-07-2005 08:29 AM

Re: scanf question
 
Quote:

Originally posted by jnusa
How would you go about to discard or overlook a section of a string, when using scanf?

I'm trying to scan a http headerline. I need to read the status code

e-g-
Code:

int status;
status atoi(scanf(hdrstr, "HTTP/1.%* %s));

will %* discard whatever the char is?

First off, you have an unterminated string constant. You want to put a close quote mark in there somewhere.

%* must be followed with a format string. For example, %*c will discard a character (like %c will read a character).

Also, scanf() returns the number of matches, not the string that was matched. To trap that, you will need to assign it to a value. scanf() can parse integers, so you might as well do that all in one go:
Code:

int status;
scanf(hdrstr, "HTTP/1.%d", &status);


jnusa 01-07-2005 08:31 AM

Thanks for the reply.

jlliagre 01-07-2005 08:37 AM

That should be:
Code:

sscanf(hdrstr, "HTTP/1.%*c %d", &status);

jnusa 01-07-2005 09:04 AM

gotcha ;)


All times are GMT -5. The time now is 06:54 AM.