Thread: sscanf problem...help please!

  1. #31
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by victor_miami
    The "value" variable is a double (please see the code below). Is there a way to assign this to an array of float type variable? (i.e. - var_value[10][1600], where 10 can be up to 10 different variables and the 1600 can be 1600 pieces of data per variable).
    Short answer: yes.
    Quote Originally Posted by victor_miami
    The reason I need to do this is that I am writing this out to a file and do not want it to appear as double precision. Or am I better off handling it when I write it to the file (at least I think there is a way to do this but I need to first figure out how).
    I think now I'd take this discussion in a different direction.

    What do you want your file format to be?
    Do you intend to have a text file or a binary file?
    Would an array of structures be more appropriate?
    How do you intend to keep track of the actual number of values?
    Etc.?
    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.*

  2. #32
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    What do you want your file format to be?
    Do you intend to have a text file or a binary file?
    Would an array of structures be more appropriate?
    How do you intend to keep track of the actual number of values?
    Etc.?"
    Dave,

    The file will be a text file. I don't know if it will make a difference if the values are written out as float or char type? Does it make any difference? Another process will eventually open this text file and read the values from it and use them.

    I currently use a structure with the variable name and their respective values.

    I keep track of the number of values through a loop for variables, so each variable that comes up in the loop then has all of it's values loaded into a value array. I'm certain there are far better ways of doing this but this is the tac I've taken (right or wrong).


    I appreciate the help!

  3. #33
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by victor_miami
    The file will be a text file. I don't know if it will make a difference if the values are written out as float or char type? Does it make any difference? Another process will eventually open this text file and read the values from it and use them.

    I currently use a structure with the variable name and their respective values.

    I keep track of the number of values through a loop for variables, so each variable that comes up in the loop then has all of it's values loaded into a value array. I'm certain there are far better ways of doing this but this is the tac I've taken (right or wrong).
    Your approach sounds quite reasonable. My simulation developed into something like this.
    Code:
    struct record
    {
       char   name[128];
       double value[1600];
       size_t length;
    };
    
    /* ... */
    
                   if ( end != field && *end == '\0' )
                   {
                      /* It's a number. */
                      rec[j].value[rec[j].length++] = value;
                   }
                   else
                   {
                      /* It's not a number. */
                      j = count++;
                      strcpy(rec[j].name, field);
                      i = 0;
                   }
    So it sounds quite similar to what you've done.

    I don't think using a float instead of a double will have much benefit for you. I was using %g in the fprintf to get rid of trailing zeros anyways.
    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.*

  4. #34
    Registered User
    Join Date
    Oct 2006
    Posts
    23
    very similar to what I have implemented. You have taught me a lot! I appreciate all the help. I know there is an area for book recomendations. Can you recommend a good C book?
    Thanks!

  5. #35
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You mean like casting ?

    Edit : Whoa...I was ridiculously beat on this thread
    Last edited by Happy_Reaper; 10-30-2006 at 08:40 PM.
    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

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