storing integers from a text file into a 1d array

This is a discussion on storing integers from a text file into a 1d array within the C Programming forums, part of the General Programming Boards category; I am new to c and i have a problem i cant seem to figure out. The problem states ..... ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    storing integers from a text file into a 1d array

    I am new to c and i have a problem i cant seem to figure out. The problem states .....

    Create a file and call it testdata21. The file should contain exactly 15 integer values. Read these 15 numbers into a one-dimensional array. then print the numbers out in reverse order from which they were entered.

    I have tried every possible thing i can think of and my cook isn't very helpful. I have for the most part removed any code that im pretty sure wasn't right and this is what i got. im pretty sure im right so far im jsut missing some things.


    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]){
    
      int number[15] ;
      FILE *Fin = NULL;
      int i;
    
      Fin =fopen("testdata21","r");
    
      for(i = 15; i >= 0; i--){
        fscanf(Fin,"%d",&number[]);
    
        printf("%d",number[]);
      }
    
      fclose(Fin);
    
      return 0 ;
    }
    Any help would be greatly appreciated thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think number[] is at all syntactically correct? Maybe you should decide which slot in the number array you want to fill?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Also, since the problem states you need to read the file first... (because you in effect need to display the last number first, etc.), you had better think about adding a loop to display the array contents AFTER you are through reading everything. The printf() you have now is fine for just debugging purposes - seeing that the file is read correctly but it doesn't address the challenge.

    While it doesn't matter in which direction you fill the array as long as you show it later in reverse order, I'd say it's more intuitive to fill the array in ascending order rather than in reverse order. Also, since you allocated 15 elements, be careful that the index ranges from 0 to 14 only. You're starting out at 15 (ouch!!).

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    8
    ok thanks guys i finnished my program and runs beautifully. i knew the code above was off i jsut needed a push in the right direction. ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Words from a text file
    By matt_570 in forum C++ Programming
    Replies: 18
    Last Post: 12-10-2008, 12:35 PM
  2. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 02:36 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21