Thread: Simple array problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    Simple array problem

    Hi,

    I have a file that look like this (for example)
    Code:
    1111 6666 0.12 1234
    2222 7777 0.13 2345
    3333 8888 0.14 3456
    4444 9999 1.15 4567
    5555 1010 0.16 5678
    What I want is to put these value into four separate array. Here's a part of my program.
    Code:
    int main()
    {     
       FILE * pFichier;   
       char line [BUFSIZ];   
       int dLimitInf;   
       int dLimitSup;   
       float fT;     
       int dK1;   
       int i = 10;   
       int j = 1; 
       int k = 1;   
       int l = 1;
       int m = 1;  
       char sLimitInfStocke[7][5];   
       char sLimitSupStocke[8][5];   
       char sTStocke[5][5];   
       char sdK1Stocke[8][5];  
    
       while (fgets (line , BUFSIZ, pFichier) != NULL)
          {   
             sscanf (line, "%d %d %f %d",&dLimitInf,&dLimitSup,&fT,&dK1);        
             sLimitInfStocke[7][j]= dLimitInf;                
             sLimitSupStocke[8][k]= dLimitSup;     
             sTStocke[5][l] = fT;      
             sdK1Stocke[8][m] = dK1;       
             j++; k++; l++; m++;
         }
         fclose (pFichier);
    The part with the array is surely wrong. First because I have a warning message that tells me converting to char from float. Also, I've tried to see if my arrays were alright, but I'm not able to print something that make sense. So I would appreciate if someone could give me an other way to do it (since I'm pretty sure this thing doesn't work) and well, if it's right how do I print these values from the array.

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm confident that your only problem is that your attempting to store good data in too small arrays.
    Code:
    sLimitInfStocke[7][j]= dLimitInf;                
    sLimitSupStocke[8][k]= dLimitSup;     
    sTStocke[5][l] = fT;      
    sdK1Stocke[8][m] = dK1;
    Not one of dLimitInf, sLimitSup, ft, or dK1 are characters, so why are you storing these in arrays of arrays of characters? Is there a reason? They probably should be stored in arrays that match their variable type.

    When you print your data, let printf take care of the conversion back to printable characters for you.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    You're right, I totally agree that I shouldn't put them in character array ( and my goal is to put them in arrays that match their variable type). But when I put
    Code:
       int sLimitInfStocke[7][5];   
       int sLimitSupStocke[8][5];   
       float sTStocke[5][5];   
       int sdK1Stocke[8][5];
    the program crash.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Forget that, everything work fine, you were right about storage in too small arrays, I tough the mistake was elsewere because it was working with char (when we are tired, these are stupid deduction that hit us)
    Last edited by nevrax; 04-16-2007 at 06:10 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while fgets actually reads something and our counter is less than five
        sscanf into a[ counter ], b[ counter ], c[ counter ] ....
        counter++
    Is that what you had in mind?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  2. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM