C programming help

Discussion in 'OT Technology' started by MaxMouse, Jun 8, 2005.

  1. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    I'm trying to read info from a file. I want to read the info in, store it, then work with it with respect to the number the line begins with.

    The problem I have is that I cannot get the loops to run through the whole file and store the numbers where I want them to be stored. Any help would be appreciated.

    These are the loops I have now.
    Code:
    while(! (feof (lottery)))
    {
    fscanf(lottery, "%d%d%d%d%d%d%d%d", &lotterytype, &a1a, &b2a, &c3a, &d4a, &e5a, &f6a, &g7a, &h8a);
    
    		for(i=1; i<=lotterytype; i++)
    		{
    			if(lotterytype == 6)
    			{
    				file61 = a1a;
    				file62 = b2a;
    				file63 = c3a;
    				file64 = d4a;
    				file65 = e5a;
    				file66 = f6a;
    				file6bonus = g7a;
    			}
    			
    			else
    			if(lotterytype == 7)
    			{
    				file71 = a1a;
    				file72 = b2a;
    				file73 = c3a;
    				file74 = d4a;
    				file75 = e5a;
    				file76 = f6a;
    				file77 = g7a;
    				file7bonus = h8a;
    			}	
                     }
    
    This is the file I'm reading from. The first column of numbers represents the type of lottery. The rest of the columns represent the winning numbers. I'm trying to read in the numbers and then store them depending on what type of lottery they are from.

    Code:
    6 01  21  23  27  40  45    38 
    6 02  05  06  12  19  36    07    
    6 05  11  20  23  27  28    42    
    6 09  10  16  18  20  49    46    
    6 06  26  28  30  33  34    49   
    6 12  19  23  24  29  44    28
    7 10  32  35  38  39  43  44    46    
    7 02  05  13  26  28  30  31    35  
    7 05  08  11  17  25  26  47    13
    6 06  15  18  19  31  36    29 
    6 12  15  16  22  29  46    17    
    6 05  21  23  26  27  29    43    
    6 09  11  18  19  21  48    47    
    6 16  26  29  40  43  44    49   
    6 12  19  23  34  39  44    22
    7 10  22  25  28  29  33  34    36    
    7 12  15  23  36  38  40  41    45  
    7 15  18  21  27  35  36  47    23
    
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Since the total number of columns depends on the number in the first column, you should first read the number in the 1st column, then decide whether to use fscanf to read 7 more numbers or 8 more numbers.
     
  3. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    I got it to read in each line, now my problem is that reading the last line twice and/or printing it out twice.
     
    Last edited: Jun 8, 2005
  4. dimins

    dimins I'll bring the beers ... OT Supporter

    Joined:
    Feb 13, 2002
    Messages:
    3,234
    Likes Received:
    0
    Location:
    Long Island, NY
    Will the file you are reading from be any larger than your example above? If not, you might want to just use fread and store the entire file in memory and work with it from there.

    http://www.mrx.net/c/source.html has an example ... scroll down to the T_fread(FILE *input) function.
     
  5. MaxMouse

    MaxMouse New Member

    Joined:
    Jun 7, 2003
    Messages:
    197
    Likes Received:
    0
    Location:
    toronto
    That is the file to be read, it will not grow or shrink and all values will stay the same. It's reading fine now, except for the fact that the last line is being read twice since when I do a test printf, it prints it twice.
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    End-of-file isn't being reached until the last call to fscanf. You should check fscanf's return value to make sure that it has actually matched the expected number of input values before proceeding.
     

Share This Page