Thread: sscanf problem...help please!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,665
    > 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
    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

  9. #9
    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.

  10. #10
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Didn't think so. Now you can do your stuff.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and of course - it is possible to check the token whether it includes '.'

    then - decide whether it is integer... It still can be stored as float, but printed with %.0f format or converted to integer during the printing...

    Of cause it will require to store the flag isInteger for each value read...
    Or - if all value supposed to be positive - values to be pritnted out as integers can be stored as negetives to indicate this flag...

    maybe thre are even better solutions... It's upto you to decide what is suffitient for you...
    Last edited by vart; 10-27-2006 at 02:11 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    here is the section of code I have.

    Code:
    int i=0;
    float fltread;
    float var_data[10][1800];
    
    while (fgets(s,sizeof(s),f)!=NULL) //read the lines of var data
    {
       while(sscanf(s,"%f",&fltread) != 0)
       {
          /*  an integer was read  */
          var_data[i] = fltread;
          i++;
       }							
    }
    I get a compilation error:
    cannot convert from 'float' to 'float [1800]'

    I don't know if I have the array sized properly. I sized it to hold up to 10 variables each having a maximum of 1800 data points. Does this look correct?

    I can't seem to fix it.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why you feel the need to have such a large array, but it doesn't matter, you're still using it wrong. A two dimensional array is an array of arrays. Thus, if you want just one element of one of those arrays, you have to use both sets of [ ]. Like so:
    Code:
    var_data[ i ][ j ] = fltread;
    That says: "Place fltread in spot j of array i."


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    Quzah,
    If I have 20 variables that I need to keep track of and each variable can have up to 1800 data points, how should I size the array. Can you explain? I greatly appreciate your help. Thanks!

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is datapoint?

    Do you mean you have to read up to 20 lines; each line can contain up to 1800 numbers?
    Do you really need to store them all or just the min/max/avg?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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