Thread: scanf and formating data

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >>You may want to try something like this to have the compiler do the math.
    >Yes, that certainly takes away the math and simplifies things.



    Yes, perhaps I should have said, "for maintenance issues -- if you ever adjust the size of an array, you only need to make one change." Or something like that. Poorly worded either way, I'll bet.
    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. #17
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    now my program is locked into an endless loop.
    Does fscanf return EOF when the file has been completely read?
    below is my updated code.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  3. #18
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    Dave_Sinkula

    I attempted to modify your code, but it doesn't get past the second batch of data.
    Code:
          char format[BUFSIZ];
          char label1[5],  label2[18], label3[14], label4[13], label5[18],
               label6[11], label7[12], label8[15], label9[7];
          sprintf(format, "%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,]"
                  ",%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],%%*c",
                  (int)(sizeof label1 - 1), (int)(sizeof label2 - 1),
                  (int)(sizeof label3 - 1), (int)(sizeof label4 - 1),
                  (int)(sizeof label5 - 1), (int)(sizeof label6 - 1),
                  (int)(sizeof label7 - 1), (int)(sizeof label8 - 1),
                  (int)(sizeof label9 - 1));
          printf("format = \"%s\"\n\n", format);
    
          while ( fscanf(file, format, label1, label2, label3, label4,
                         label5, label6, label7, label8, label9) == 9 )
          {
             /* ... */
          }
    below is my modified version
    Code:
             sprintf(buffer, "%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],"
                     "%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],%%%d[^,],",
                     (int)(sizeof(data[x].name)-1),(int)(sizeof(data[x].PeriTime)-1),
                     (int)(sizeof(data[x].PeriAU)-1),(int)(sizeof(data[x].Eccent)-1),
                     (int)(sizeof(data[x].LongPeri)-1),(int)(sizeof(data[x].LongNode)-1),
                     (int)(sizeof(data[x].Inclin)-1),(int)(sizeof(data[x].Semi)-1),
                     (int)(sizeof(data[x].Period)-1),(int)(sizeof(data[x].MPC)-1));
    
    //line 58
             while(fscanf(infile,buffer,data[x].name,data[x].PeriTime,data[x].PeriAU,
                          data[x].Eccent,data[x].LongPeri,data[x].LongNode,data[x].Inclin,
                          data[x].Semi,data[x].Period,data[x].MPC)==10)
             {
    
                fscanf(infile,buffer,data[x].name,data[x].PeriTime,data[x].PeriAU,
                       data[x].Eccent,data[x].LongPeri,data[x].LongNode,data[x].Inclin,
                       data[x].Semi,data[x].Period,data[x].MPC);
    
                x++;
             }
    what did i do wrong?
    below is my output.
    Name
    Perihelion time
    Perihelion AU
    Eccentricity
    Long. perihelion
    Long. node
    Inclination
    Semimajor axis
    Period

    2P/Encke
    2003-12-29.878
    Last edited by xviddivxoggmp3; 04-15-2004 at 04:08 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think you may want to use "%%%d[^\n]%%*c" for the last element, since the lines don't end with commas. Also, check your PMs.
    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.*

  5. #20
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    The while loop never tests true

    I noticed when trying to find what is wrong the while loop never tests true.
    Code:
             while(fscanf(infile,buffer,
                          data[x].name,
                          data[x].PeriTime,
                          data[x].PeriAU,
                          data[x].Eccent,
                          data[x].LongPeri,
                          data[x].LongNode,
                          data[x].Inclin,
                          data[x].Semi,
                          data[x].Period,
                          data[x].MPC)==10)
    Does it not like an array of structures?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #21
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    It was the size of my arrays.
    Apparantly fscanf() will not register a succesful read if it is placed into an incorrectly sized array.
    Dave_Sinkula
    you rock.
    Thanks for all the help.
    Check out your reputation.
    how ever you do that
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  7. #22
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I spoke to soon on thinking the program was completed.
    I have two lines of data I'm testing a reading.
    The two differ from each other.
    P/1996 R2 (Lagerkvist),2004-6-7.4051,2.622945,0.308525,334.2622,40.2251,2.6022,3 .79326,7.39 years, MPC 42666
    C/1997 BA6 (Spacewatch),1999-11-28.0414,3.428902,0.998982,285.8451,317.6646,72.758 8,,, MPC 45960
    I have my program written to accept the first line of the two, but not the second.
    I attempted to change to fscanf() format to accept the tripple comma in the second line, but with no luck.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  8. #23
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    ne pensez pas de cette facon car cela est fastisieux
    mais utiliser la library <fstream.h>
    pour lagestion de vos fichier , suppression ,creation,ou meme controle si ils existes ou aps
    ....
    c'est toujour plus facile que d'utiliser scanf qui est approprier a lire sous stdio.h
    ****==>attention utiliser %c au lieu de %s c'est plus facile (mais en tenant compte d'une condition de repetition pour l'espace qui n'est aps prix en compte)

  9. #24
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    fstream

    Sorry I do not speak french.
    I unfortunately am restricted on the project of what I can use.
    fstream.h is not allowed for this project.
    I know of many easier ways, using C++, but that is restricted for this project also.

    ----------------below is weak free french translation.------------------

    Désolé je ne parle pas le français. Je suis malheureusement limité sur le projet de ce que je peux utiliser. Le fstream.h n'est pas tenu le compte de ce projet. Je sais de beaucoup de façons plus faciles, le C d'utilisation + +, mais cela est limité pour ce projet aussi.

    I'm sure that is not correct. Sorry.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed