Thread: Importing File to Array

  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 10:42 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    How did you declare the matrix 2D array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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