Thread: sscanf problem...help please!

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    sscanf problem...help please!

    I'm having trouble reading a data file's data using sscanf. The format of the data file (called file.txt) looks like this:

    NUMBER OF PARAMS 249
    TITLE BLOCK
    B211W111
    444-565AG
    FIGURE 3.6-1a
    3-46
    NORMAL RUN
    COWL 20, NORMAL MODE
    CONDITION 6.18-1
    PARAMS
    time 10 90 8.0 3.0 6.0
    speed 10 190 9.0 3.0 6.0
    height -100 350 9.0 15.0 6.0
    time 10 90 8.0 3.0 1.0
    distance0 200000 4.0 3.0 1.0
    time 10 90 8.0 3.0 1.0
    distance 0 200000 4.0 3.0 1.0


    My code should skip down to the "PARAMS" section (which it does) and then it is supposed to read the first variable name and store it (in junk), then read the next two fields (min and max) following the variable name. The problem is that it is not storing the min and max correctly.

    Thanks in advance!

    My code looks like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       FILE *f;
        char junk[20][10], vars[20][10], min[20][10], max[20][10];
        char * pch, * pch1, s[500];
        int i = 0;
        f=fopen("file.txt","r");
        if (!f)
          return 1;
        //find the first variable for min and max data
        while (fgets(s,sizeof(s),f)!=NULL)
        {
    	// skip down to the "PARAMS" line
    		pch = strstr (s,"PARAMS");
    		if(pch != NULL)
    		{
    		    // find each variable
    		    while (fgets(s,sizeof(s),f)!=NULL)
    			{
    			    pch1 = strstr (s,vars[i]);
    			    if(pch1 != NULL)
    			    {
    				// record each variables min and max data
    				printf("variable name = %s\n",vars[i]);
    				sscanf( s, "%s %s %s", junk[i], min[i], max[i]);
    				printf("min = %s\n",min[i]);
    				i++;
    			    }
    			}
    		}
          }
          fclose(f);
    
          return 0;
    }
    Last edited by victor_miami; 10-27-2006 at 07:19 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    I fixed that problem. Sorry it was a stupid mistake I should have caught in the first place. I have another problem. What is the best way to read/store data that is in the following format?
    var1
    var1_data
    var2
    var2_data
    .
    .
    .
    var(n)
    var(n)_data

    where, var(n)_data is separate data points of integer and real numbers. There can be as many as 2000 data points per var(n). I don't want to store the var(n) because I already have it. I need to store only it's data. Will sscanf do it or is there a better function for this?
    Last edited by victor_miami; 10-26-2006 at 10:29 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > pch1 = strstr (s,vars[i]);
    So what sets up vars[i] in the first place?
    What are you comparing with?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    Sorry about that. I set it up by opening another file with a variable name list such as:

    var1 var2 var3 ...var(n). I use sscanf to read each variable name into an array of variable names. I actually use a version that you posted earlier this week and modified it slightly for my need.

    1. How can I use sscanf to process numerous lines of numerical (mixed integer and real numbers) data. The # of data points on each line varies and the number of lines vary.
    2. How do I count the number of data points for each var?

    The format looks like this:

    time
    9 9.05 9.1 9.15 9.2 9.25 9.3 9.35 9.4 9.45 9.5
    9.55 9.6 9.65 9.7 9.75 9.8 9.85
    9.9 9.95 10 10.05 10.1 10.15 10.2 10.25 10.3
    10.35 10.4 10.45 10.5 10.55 10.6 10.65 10.7 10.75
    10.8 10.85 10.9 10.95 11 11.05 11.1 11.15 11.2
    11.25 11.3 11.35 11.4 11.45 11.5 11.55 11.6 11.65
    11.7 11.75 11.8 11.85 11.9 11.95 12 12.05 12.1
    12.15 12.2 12.25 12.3 12.35 12.4
    12.45 12.5 12.55 12.6 12.65 12.7 12.75 12.8 12.85 12.9 12.95 13
    13.05 13.1 13.15 13.2 13.25 13.3 13.35 13.4 13.45
    13.5 13.55 13.6 13.65 13.7 13.75 13.8 13.85 13.9
    13.95 14 14.05 14.1 14.15 14.2 14.25 14.3 14.35
    14.4 14.45 14.5 14.55
    14.6
    speed
    10.2519 10.2519 10.2519
    10.2392 10.2392 10.2392 10.2265 10.2265 10.2265
    10.2137 10.2137 10.2009 10.2009 10.1881 10.1881 10.1881 10.1753 10.1753
    10.1753 10.1753 10.1753 10.1753 10.1753 10.1625 10.1625 10.1625 10.1497
    10.1497
    10.1497 10.1368 10.1368 10.1368 10.1239 10.1239 10.1239 10.1239
    10.1111 10.1111 10.1111 10.1111 10.0981 10.0981 10.0981 10.0981 10.0852
    10.0852 10.0852 10.0852 10.0852
    10.0852 10.0723 10.0723 10.0723 10.0723
    10.0593 10.0593 10.0593 10.0464 10.0464 10.0464 10.0464
    10.0464 10.0204
    9.70302
    .
    .
    .
    more of the same



    Thanks for helping!
    Last edited by victor_miami; 10-27-2006 at 07:18 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    any help is sincerely appreciated.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This doesn't store anything, but perhaps it will give you some ideas.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int foo(const char *filename);
    
    int main()
    {
       foo("file.txt");
       return 0;
    }
    
    int foo(const char *filename)
    {
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char line[BUFSIZ];
          int j = 0, reading = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( strncmp(line, "PARAMS", 6) == 0 )
             {
                reading = 1;
             }
             else if ( reading )
             {
                double value;
                int n;
                char field[20], *end, *number = line;
                while ( sscanf(number, "%19s%n", field, &n) == 1 &&
                        (value = strtod(field, &end)), 
                        end != field && (*end == '\n' || *end == '\0') )
                {
                   number += n;
                   j += printf("%s ", field); /* being chintzy with screen space */
                   if ( j > 72 )
                   {
                      j = 0;
                      putchar('\n');
                   }
                }
             }
          }
          fclose(file);
          return 1;
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    Is the NUMBER_OF_PARAMETERS 249 field supposed to help you know the number or parameters so you can malloc that amount?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    23

    Smile

    Dave thanks for your reply. I fixed that problem. See earlier reply. I'm not looking for anyone to write my code for me. I need help figuring out which c lib functions are appropriate to solve the problems described below.

    My new problem is reading lines of data (see the most recent example I posted). The lines are all different...meaning the number of data points on each line varies (there could be only one, there could be as many as 15). The data is a mixture of real and integer numbers all separated by spaces, which may not be an issue since I would store them in a character array. Then I need to recognize when the data ends (the data is always ended by another variable followed by it's data.

    Thank you for helping!
    Last edited by victor_miami; 10-27-2006 at 08:22 AM.

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    sscanf returns the amount of inputs it managed to read. Therefore, you could just create a loop for every line you get in fgets, and iterate through that line, reading numbers, untill sscanf returns 0.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    OK I'm clueless on how to do this. I can setup the reading of each line of data using fgets but I am not sure how to set sscanf to read the intergers and real numbers, and then to stop reading when the data is finished. Could you show an example using sscanf to do that...please?
    Last edited by victor_miami; 10-27-2006 at 08:41 AM.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Here's an example that would read some integers :

    Code:
    /*  Assumes a line was read from fgets and was put into buffer*/
    int data[100];
    int intread;
    int i = 0;
    
    while(sscanf(buffer,"%d",&intread) != 0)
    {
    	/*  an integer was read  */
    	data[i] = intread;
    	i++;
    }
    Get the idea ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    OK I understand that but I'm not sure if it stops when it hits a non number. There are two issues I need to solve. One is reading integers and reals from the same line. The other is to know when to stop reading the data (each data group is followed by the next variable name and then it's data).

    How can sscanf also look for real numbers at the same time?

    I apologize for the annoying cut and paste of previous posts but I need to do this to illustrate what the data looks like.


    The format looks like this:

    time
    9 9.05 9.1 9.15 9.2 9.25 9.3 9.35 9.4 9.45 9.5
    9.55 9.6 9.65 9.7 9.75 9.8 9.85
    9.9 9.95 10 10.05 10.1 10.15 10.2 10.25 10.3
    10.35 10.4 10.45 10.5 10.55 10.6 10.65 10.7 10.75
    10.8 10.85 10.9 10.95 11 11.05 11.1 11.15 11.2
    11.25 11.3 11.35 11.4 11.45 11.5 11.55 11.6 11.65
    11.7 11.75 11.8 11.85 11.9 11.95 12 12.05 12.1
    12.15 12.2 12.25 12.3 12.35 12.4
    12.45 12.5 12.55 12.6 12.65 12.7 12.75 12.8 12.85 12.9 12.95 13
    13.05 13.1 13.15 13.2 13.25 13.3 13.35 13.4 13.45
    13.5 13.55 13.6 13.65 13.7 13.75 13.8 13.85 13.9
    13.95 14 14.05 14.1 14.15 14.2 14.25 14.3 14.35
    14.4 14.45 14.5 14.55
    14.6
    speed
    10.2519 10.2519 10.2519
    10.2392 10.2392 10.2392 10.2265 10.2265 10.2265
    10.2137 10.2137 10.2009 10.2009 10.1881 10.1881 10.1881 10.1753 10.1753
    10.1753 10.1753 10.1753 10.1753 10.1753 10.1625 10.1625 10.1625 10.1497
    10.1497
    10.1497 10.1368 10.1368 10.1368 10.1239 10.1239 10.1239 10.1239
    10.1111 10.1111 10.1111 10.1111 10.0981 10.0981 10.0981 10.0981 10.0852
    10.0852 10.0852 10.0852 10.0852
    10.0852 10.0723 10.0723 10.0723 10.0723
    10.0593 10.0593 10.0593 10.0464 10.0464 10.0464 10.0464
    10.0464 10.0204
    9.70302
    .
    .
    .
    more of the same


    Thanks!

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Those all look like floats to me. So what you'd want to do is fgets every line. Then see if it's a word. If it's a word, deal with accordingly. If it isn't, then read the data in using what I showed you and place it in the apporiate place.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    happy...look at the data, some of the data is integer data.

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    ok, but will the trailing zeroes cause a problem ?

    What I mean is, it will still read 9, it will probably just store it as 9.000 or something of the sort
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    No the trailing zeros are ok. I also need to be able to detect when the data ends as shown in the example of the data I used. The data needs to be read into an array of variable data for up to 100 variables. So I would need to read it into say var_data[10][100]?

    Thanks!
    Last edited by victor_miami; 10-27-2006 at 01:58 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  3. sscanf problem
    By LordShme in forum C Programming
    Replies: 5
    Last Post: 12-05-2006, 09:09 PM
  4. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM