question about sscanf

Posted on 2006-04-30
Last Modified: 2010-04-15
Hello experts,
Currently i have a method with code like this:
  sscanf(s, "%lf %lf %lf %lf %d %d ", &x, &y, &z, &weight, &se, &sl);   --- m1
this will read in the info about some 3D pionts from a file 'output'.
Now i am going to add one more property of the 3D points called 'name' and 'name' will be written to the 'output' file, so i changed the code to this:
   char name[20];
   sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, &name);  -- m2
The problem happens because not all 'output' files have 'name' saved. I want to add some checking: if there is a 'name', use m2 otherwise use m1.
Could anyone give me some idea how to add such a checking? How do i check 'name' exists?
Question by:lzha022
    LVL 45

    Assisted Solution

    Hi lzha022,

    Use method 2.   If the first character of name is a new-line character or the end-of-string character, then no name was present on the line.

    Good Luck!
    LVL 1

    Expert Comment

    If your file is a text file where you have 'name' as a stand alone label, you can search for it. For example, you can search the file line by line and if you find 'name', use your sscanf function, but that is if 'name' is in the file in its own line:

    #include <stdio.h>

    int searchString(FILE *fd, char name[25])
         char buffer[20];
         /* search through the file line by line.... */
         while ( (fgets(buffer, 20, fd)) != NULL )
              if( strcmp(buffer,"name\n") == 0 )
                   return 1;
         return 0;

         FILE *fd;
         char name[] = "name\n";
         char filename[] = "searchFile.txt";
         if ( (fd = fopen(filename, "r")) == NULL )
              printf("Error %s\n", filename);
              return 1;

         if( searchString(fd, name) )
            sscanf(s, "%lf %lf %lf %lf %d %d ", &x, &y, &z, &weight, &se, &sl);   \*--- m1  */
            sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, &name); \*--- m2  */
         /* depending on your compiler you might not need the system() function */
         /*system("pause"); */
         return 0;

    LVL 16

    Accepted Solution

    Hi lzha022,

    The trick to use here is to make use of the return value of scanf. I believe it returns the number of parameters matched or the number of values filled (there is a difference).

       int nScanned = sscanf(s, "%lf %lf %lf %lf %d %d %s", &x, &y, &z, &weight, &se, &sl, name);  -- m2 // Note that I have changed &name to 'name' as it is likely to be a char*.

    Obviously you can now do

    switch ( nScanned )
      case 6: // No name read.
      case 7: // Read a name from the file.


    Author Comment

    Thanks all for your reply.
    I forgot to mention, 'name' variable is entered by user so i have no way to know what it is.
    Kdo`d method sounds good, i will try this first.
    Thanks again

    Author Comment

    Hi Paul,
    I just read your comments. If this can work, it would be the easist way. I will try it now.


    Author Comment

    Thanks everyone. Paul`s method is really easy. Kdo`s method will also work i think though i have not tried it yet.
    Thanks again.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    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 this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    729 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

    19 Experts available now in Live!

    Get 1:1 Help Now