Thread: Convert .txt file to array

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Convert .txt file to array

    I was wondering if anyone can help me - I don't really know what I'm doing with strings, and have been struggling to work this one out on my own.

    I have a .txt file consisting of 10 lines of 15 numbers per line (some 1 digit, some 2), with numbers separated by spaces. I want to put these numbers into an array, eg. I want an array

    int numbers[10][15];

    where numbers[i][j] has the value of the jth number from the ith line of my .txt file.

    How can I go about doing this? Please try and keep things fairly basic

    Cheers

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Read in one line at a time using fgets() and then loop thru it with a simple sscanf:
    Code:
    int data[10][15], i, j;
    char line[256];
    for(i=0;i<10;i++) {
        fgets(line,256,fd);
        j = 0;
        while(sscanf(line,"%d%*c"),data[i][j++]);
    }
    The %*c will just chuck a space or other single character -- it may work with out that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    fgets(), then sscanf() i think.

    fgets will get the line, sscanf can get numbers from that line.

    mk's fast.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Code:
    int MainCL(void)
    {
      int data[10][15],i,j;
      char line[256];
      FILE *file;  
      file = fopen("test.txt", "r"); 
      for ( i=1 ; i<=10 ; i++ )
        {
          fgets(line,256,file);
          j = 0;
          while(sscanf(line,"%d%*c"),data[i][j++]);
        }
      return 0;
    }
    Thanks for the quick responses.
    The above code is the exact code I have tried, but when I try to run this the program just automatically terminates itself (I have tried putting a simple print command at the end to see if it gets that far, but it doesn't make it). Any suggestions?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Array's are zero based so a 10 element array for example has indexes from 0 to 9. Your loop goes from 1 to 10 which goes past the end of the array.

    That, plus the sscanf part is incorrect.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ah yes, should have spotted the array problem.

    But this is the first time I have seen sscanf, how exactly can I correct this part? From what I can make out, the 'data' part should be in the sscanf brackets as well, but I still can't get anything to work

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM