Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


fscanf problem

Posted on 2007-07-26
Medium Priority
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
  • 2
LVL 85

Expert Comment

ID: 19575667
fscanf( f,
LVL 46

Expert Comment

by:Kent Olsen
ID: 19576483

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

ID: 19576878
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 85

Accepted Solution

ozo earned 200 total points
ID: 19577033
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

578 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