Importing File to Array

This is a discussion on Importing File to Array within the C Programming forums, part of the General Programming Boards category; So I'm trying to inport a file into an array. The file is of known size, containing 9 numbers (ie ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    26

    Importing File to Array

    So I'm trying to inport a file into an array. The file is of known size, containing 9 numbers (ie a 3x3 matrix) separated by spaces and new lines. I need to take this file, and turn it into an array. So far, I have the matrix.dat that looks like:
    Code:
    4 -5 3
    6 -9 3
    4 8 -1
    and I'm trying to import this using nestled for loops like:#
    Code:
    input = fopen("matrix.dat", "r");
    if(input == (FILE*) NULL)
    {
    	printf("Can not open file matrix.dat\n");
    	exit(EXIT_FAILURE);
    }
    else
    {
    	for(i=0; i<3; i++)
    	{
    		for(j=0; j<3; j++)
    		{
    			fscanf(input, "%f", &matrix[i][j]);
    		}
    	}
    	printf("%s", matrix);
    }
    exit(EXIT_SUCCESS);
    Now i'm assuming one of two parts is the problem. Either my for loops are inproperly set up, or (as I am assuming) the 'fscanf' is wrong. The problem is, I'm not sure how to fscanf for multiple dimensional arrays. Any help?

    Cheers
    Last edited by tomeatworld; 11-21-2010 at 09:42 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    How does it not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    I get a seg fault when run!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    How did you declare the matrix 2D array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    I've managed to fix it. I'd defined my matrix with dimensions i and j which meant it didn't have enough memory (i think). I've got it working now! Thanks anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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