scanf problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Lam

    #1

    scanf problem

    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.e xe

    however, when I read the value by

    sscanf(read_buf fer,"file:%s",S RecordFile);


    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
  • Michael Mair

    #2
    Re: scanf problem



    Ivan Lam wrote:[color=blue]
    > 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.e xe
    >
    > however, when I read the value by
    >
    > sscanf(read_buf fer,"file:%s",S RecordFile);
    >
    >
    > I only get "c:\program ", I know this is caused by %s only input
    > non-whitespace characters. How can I get around it?[/color]

    Use scansets.
    If your value stands on its own, that is, the C string version
    of the above is
    "file:c:\\progr am files\\mpd\\mpd .exe\n"
    then you can use
    sscanf(read_buf fer,"file:%[^\n]",SRecordFi le);
    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, &SRecordFileExt ension[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(SRecord File) >= size - 5) {
    /* Error handling: SRecordFile is not big enough */
    }
    else {
    SRecordFileExte nsion[0] = '.';
    strcat(SRecordF ile, SRecordFileExte nsion);
    }
    }
    If the pattern gets more complicated (you do not have extensions),
    then you will have to do it by hand.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.

    Comment

    • Guy

      #3
      Re: scanf problem

      Maybe you can simply use

      strncpy(SRecord File, read_buffer + 5, strlen(read_buf fer) - 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 =-----

      Comment

      • Lawrence Kirby

        #4
        Re: scanf problem

        On Wed, 22 Dec 2004 02:33:35 -0800, Ivan Lam wrote:
        [color=blue]
        > 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.e xe
        >
        > however, when I read the value by
        >
        > sscanf(read_buf fer,"file:%s",S RecordFile);
        >
        >
        > I only get "c:\program ", I know this is caused by %s only input
        > non-whitespace characters. How can I get around it?[/color]


        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

        Comment

        • Richard Bos

          #5
          Re: scanf problem

          ivanlkc@hkem.co m (Ivan Lam) wrote:
          [color=blue]
          > 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.e xe
          >
          > however, when I read the value by
          >
          > sscanf(read_buf fer,"file:%s",S RecordFile);
          >
          > I only get "c:\program ", I know this is caused by %s only input
          > non-whitespace characters. How can I get around it?[/color]

          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_b uffer, "file:", 5)==0)
          strcpy(SRecordF ile, 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=rea d_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

          Comment

          Working...