fscanf problem

Posted on 2007-07-26
Last Modified: 2010-04-01
I am trying to use fscanf. I have a text file which contents - test|localhost||clienthost|4350|testUser|test|login|No Message

I have struct which is -
typedef struct _packetFormat
   char ipServerAddress[100];
   char ipServerName[100];
   char ipClientAddress[100];
   char ipClientName[100];

   int portNum;
   char userName[100];
   char password[100];
   char command[100];
   char message[5000];


fscanf( f,

I am trying to parse the file and populate the member variables of the struct. For some reason, I am not getting the correct values.  ipServerAddress should have " test". How can I achieve this with fscanf(), any other alternatives?
Question by:openujs
    LVL 84

    Expert Comment

    fscanf( f,
    LVL 45

    Expert Comment


    Hi openujs,

    One of the most frustrating aspects of formatted input in C is that %s doesn't do what anyone expects, except possibly the C developers.

    As ozo demonstrates, you'll need a format string that matches your data and %s isn't appropriate for your data.


    Author Comment

    I used the above fscanf, program is crashing at fscanf stmt. Also, I was thinking you need %s for string, what does [^|] do? why using '|' twice? Explanation would be nice. Thanks both of you :)
    LVL 84

    Accepted Solution

    Did you supply a pointer for the %d?

         [     Matches a nonempty sequence of characters from the specified set of
               accepted characters; the next pointer must be a pointer to char,
               and there must be enough room for all the characters in the string,
               plus a terminating NUL character.  The usual skip of leading white
               space is suppressed.  The string is to be made up of characters in
               (or not in) a particular set; the set is defined by the characters
               between the open bracket [ character and a close bracket ] charac-
               ter.  The set excludes those characters if the first character
               after the open bracket is a circumflex ^.  To include a close
               bracket in the set, make it the first character after the open
               bracket or the circumflex; any other position will end the set.
               The hyphen character - is also special; when placed between two
               other characters, it adds all intervening characters to the set.
               To include a hyphen, make it the last character before the final
               close bracket.  For instance, `[^]0-9-]' means the set ``everything
               except close bracket, zero through nine, and hyphen''.  The string
               ends with the appearance of a character not in the (or, with a cir-
               cumflex, in) set or when the field width runs out.

               If an l qualifier is present, the next pointer must be a pointer to
               wchar_t, into which the input will be placed after conversion by

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now