scanning numbers in files into arrays

This is a discussion on scanning numbers in files into arrays within the C Programming forums, part of the General Programming Boards category; Hi guys, Can someone explain to to scan the contents of a text file into an array? For example, I ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    scanning numbers in files into arrays

    Hi guys,
    Can someone explain to to scan the contents of a text file into an array? For example, I have a text file with the numbers 4 34 64 23 and 73 in it, and I need to copy those numbers into an array. I'm having trouble figuring out how to do this.
    Thanks for all your help!
    John

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    This is working for me for 2d array in the function below >

    Code:
    void LoadGame(GamePieces solpeg[MAXDWN][MAXACR])
    {
        int dwn, acr = 0;
    
        ifstream Gamefile("mysave.txt");
    
        for(dwn = 0; dwn < MAXDWN; dwn++)
        {
            for(acr = 0; acr < MAXACR; acr++)
            {
                Gamefile >> solpeg[dwn][acr].areatype;
            }
        }
        Gamefile.close();
    }
    obviously 1d array you just need single loop.
    my array here is an array of objects and is reading the file into the data member integer areatype, the source file has the values seperated by spaces.

    also > http://www.cprogramming.com/tutorial/lesson10.html

    sorry i just realised this is a C forum post, apologies
    Last edited by rogster001; 11-12-2009 at 03:48 AM. Reason: wrong forum

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    heres a part of a working c function i did

    Code:
    FILE *NumData;
    int nStore = 0;
    NumData = fopen("last8live.txt", "r");
        
        if(!NumData) 
        {
               printf("ERROR File last8live.txt not found!\n\n");
               getch();
        }
        
        while (fscanf(NumData, "%d",&num) != EOF) 
        {             
               lastdraw[nStore] = num;                  
               nStore++;
         }

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    check this out

    Code:
    #include <stdio.h>
    
    #define LEN 10
    
    int main(const int argc, char** argv) {
      FILE* fd = 0;
      if ((fd = fopen(*(argv + 1), "r")) == 0) {
        printf("[Error] While opening [%s] file\n", *(argv + 1));
      } else {
        char * str = (char*)malloc(LEN);
        memset(str, 0, 10);
        while (fgets(str, LEN, fd) != 0) {
          printf("%d\n", atoi(str));
        }
      }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  2. Outputting numbers in arrays
    By rachael033 in forum C++ Programming
    Replies: 10
    Last Post: 05-29-2007, 02:56 AM
  3. Replies: 4
    Last Post: 04-19-2005, 08:05 PM
  4. reading files into arrays
    By SMB3Master in forum C Programming
    Replies: 12
    Last Post: 10-18-2003, 08:23 PM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM

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