scanf problem

I

Ivan Lam

Hi All,

Thanks for reading my post!

I have a problem that using the scanf function. I would like to scan a
value from a line like:

file:c:\program files\mpd\mpd.exe

however, when I read the value by

sscanf(read_buffer,"file:%s",SRecordFile);


I only get "c:\program", I know this is caused by %s only input
non-whitespace characters. How can I get around it?

Thanks for your help!

Ivan
 
M

Michael Mair

Ivan said:
Hi All,

Thanks for reading my post!

I have a problem that using the scanf function. I would like to scan a
value from a line like:

file:c:\program files\mpd\mpd.exe

however, when I read the value by

sscanf(read_buffer,"file:%s",SRecordFile);


I only get "c:\program", I know this is caused by %s only input
non-whitespace characters. How can I get around it?

Use scansets.
If your value stands on its own, that is, the C string version
of the above is
"file:c:\\program files\\mpd\\mpd.exe\n"
then you can use
sscanf(read_buffer,"file:%[^\n]",SRecordFile);
which reads everything except for '\n' into SRecordFile.
It might be a good idea to restrict the maximum number of characters
which can be read; otherwise, the buffer SRecordFile may overflow.

If the whole thing does not stand on its own, you have to utilise
the ".exe" or comparable extensions:
if(sscanf(read_buffer,"file:%[^.\n].%3s",
SRecordFile, &SRecordFileExtension[1]) != 2)
{
/* Error handling: we could not match the pattern with the
** extension */
}
else {
/* Check first whether there still is enough left for the
** extension; say, size is the size of the buffer */
if (strlen(SRecordFile) >= size - 5) {
/* Error handling: SRecordFile is not big enough */
}
else {
SRecordFileExtension[0] = '.';
strcat(SRecordFile, SRecordFileExtension);
}
}
If the pattern gets more complicated (you do not have extensions),
then you will have to do it by hand.


Cheers
Michael
 
G

Guy

Maybe you can simply use

strncpy(SRecordFile, read_buffer + 5, strlen(read_buffer) - 5);

rather than using sscanf


~ ªÑ²¼»ù®æ¦³¤É¦³¶^, ¶R½æ­n¯à©Ó¾á­·ÀI ~

~ Samba, more than a low cost File and Printer server ~

-- Let us OpenSource --


-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
 
L

Lawrence Kirby

Hi All,

Thanks for reading my post!

I have a problem that using the scanf function. I would like to scan a
value from a line like:

file:c:\program files\mpd\mpd.exe

however, when I read the value by

sscanf(read_buffer,"file:%s",SRecordFile);


I only get "c:\program", I know this is caused by %s only input
non-whitespace characters. How can I get around it?


If you're reading line based data you'll make life a lot easier if you
read it a line at a time using fgets(). Just remember that fgets()
leaves the \n line terminator character in. Once you have the line as a
string you have all of C's string handling facilities available to you to
break it up. Other replies have made some suggestions.

Lawrence
 
R

Richard Bos

I have a problem that using the scanf function. I would like to scan a
value from a line like:

file:c:\program files\mpd\mpd.exe

however, when I read the value by

sscanf(read_buffer,"file:%s",SRecordFile);

I only get "c:\program", I know this is caused by %s only input
non-whitespace characters. How can I get around it?

First, do you mean scanf() or sscanf()? If you mean scanf(), as you say
twice, I agree with Lawrence: use fgets().
If you mean sscanf(), as your code seems to indicate, then in this
simple case I wouldn't use sscanf() at all. Use strcpy(), strncat(), or
related functions.
Assuming (as you do above; do make sure the assumption is correct before
the buffer overflow snark bites your head off) that SRecordFile is large
enough to contain the resulting string, you can do

if (strncmp(read_buffer, "file:", 5)==0)
strcpy(SRecordFile, read_buffer+5);
else
/* The string is not of the expected format;
do something else with it. */

If you already know that the string starts with "file:", you can
obviously skip the strncmp() and use just the strcpy() line.

If you only need this one line for a short while, and you don't need to
save it after you read a new one, you can even save yourself the trouble
of copying and do

char *SRecordFile;

SRecordFile=read_buffer+5;

Of course, now SRecordFile points into the same array as read_buffer, so
any change in one file name is going to occur in the other, as well.
That means that this solution isn't often the right one, but when it is,
it's probably the best way.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

scanf() 2
Scanf Problem 5
question about scanf 11
Problem with scanf 7
advanced scanf 2
strange scanf 1
Program to find the largest integer element of an array. 1
scanf and ampersand 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top