LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-07-2005, 07:55 AM   #1
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Rep: Reputation: 15
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?

Last edited by jnusa; 01-07-2005 at 08:07 AM.
 
Old 01-07-2005, 08:29 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
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);
 
Old 01-07-2005, 08:31 AM   #3
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
Thanks for the reply.
 
Old 01-07-2005, 08:37 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
That should be:
Code:
sscanf(hdrstr, "HTTP/1.%*c %d", &status);
 
Old 01-07-2005, 09:04 AM   #5
jnusa
Member
 
Registered: Jul 2004
Location: Denmark, Aarhus
Distribution: Ubuntu, Suse
Posts: 98

Original Poster
Rep: Reputation: 15
gotcha
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf question exvor Programming 5 07-24-2005 04:36 PM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM
array of structures scanf entry question ... purpleburple Programming 21 10-01-2002 11:08 PM
the perils of scanf .......I purpleburple Programming 13 09-03-2002 08:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration