Thread: reading in data into an array of arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    reading in data into an array of arrays

    Hey,

    I have 0 experience in C (although I know R somewhat well and know a little with Perl) and was assigned a completely unreasonable assignment in my R computing class. I'm trying to read in a text file into C as a multidimensional array. The data is 100 rows by 6 columns and contains positive and negative values with decimals (ie -1.24354). I want to store this data in C so that data[0][0] corresponds with the value in the first row and column.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    #define nRow 100
    #define nCol 6
    
    int main()
    {
    	int i,j;
    	double data[nRow][nCol];
    	
        FILE *fpIn = NULL;
    	fpIn = fopen("data.txt", "r");
    	if(fpIn == NULL)
    	{
    		printf("Cannot open file!\n");
    		getchar();
    		return 1;
    	}
    	else
    	{
    		printf("File is now open. Press Enter...\n");
    	}
        
        for(i = 0; i < nRow; i++) {
            for(j = 0; j < nCol; j++) {
                fscanf(fpIn, "%d", &data[i][j]); 
            }       
        }     
        printf("file has been read in\n"); 
        printf("the first entry is:\n");
        printf("%d\n", &data[1][0]);
           
        getchar();  
        fclose(fpIn);                 
    	
    	return 0;	
    }
    The value is supposed to be -1.12, but compiling and running the code returns: 2288848. When I change %d into %s, it reads the correct string, but I want these numbers as doubles. What exactly I am doing wrong?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    What you can do read the data from the file descriptor fpIn into an array of chars char*

    and then convert it to float, double or int what ever you want


    with these functions


    atoi()
    atof()
    atod()

    defined in <stdlib.h>

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you see the problem with this?

    Code:
    	if(fpIn == NULL)  //if the file can't be opened
    	{
    		printf("Cannot open file!\n");
    		getchar();
    		return 1;
    	}
    	else                     //your entire program has to fit inside this else statement, 
    	{                       //which you don't need at all.
    		printf("File is now open. Press Enter...\n");
    	}
    
    
        
        for(i = 0; i < nRow; i++) {
            for(j = 0; j < nCol; j++) {
                fscanf(fpIn, "%d", &data[i][j]); 
            }       
        }
    You need to start printing up your values from data[0][0], instead of data[1][0], as above.
    Last edited by Adak; 10-20-2009 at 11:40 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm thinking you missed "return 1;", there, Adak.

    And doubles is "%lf".

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Adak View Post
    Do you see the problem with this?

    Code:
    	if(fpIn == NULL)  //if the file can't be opened
    	{
    		printf("Cannot open file!\n");
    		getchar();
    		return 1;
    	}
    	else                     //your entire program has to fit inside this else statement, 
    	{                       //which you don't need at all.
    		printf("File is now open. Press Enter...\n");
    	}
    
    
        
        for(i = 0; i < nRow; i++) {
            for(j = 0; j < nCol; j++) {
                fscanf(fpIn, "%d", &data[i][j]); 
            }       
        }
    Did you read the code carefully the logic is correct dude. See the original code

    Code:
    int main()
    {
    	int i,j;
    	double data[nRow][nCol];
    	
        FILE *fpIn = NULL;
    	fpIn = fopen("data.txt", "r");
    	if(fpIn == NULL)
    	{
    		printf("Cannot open file!\n");
    		getchar();
    		return 1;
    	}
    	else
    	{
    		printf("File is now open. Press Enter...\n");
    	}
        
        for(i = 0; i < nRow; i++) {
            for(j = 0; j < nCol; j++) {
                fscanf(fpIn, "%d", &data[i][j]); 
            }       
        }     
        printf("file has been read in\n"); 
        printf("the first entry is:\n");
        printf("%d\n", &data[1][0]);
           
        getchar();  
        fclose(fpIn);                 
    	
    	return 0;	
    }

    File Opening is done before the if else condition in the else part there is only a printf statement

    Do you agree it will work


  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I missed the "return 1" at first, and wrote a post.

    Then I edited the post right afterward, when I saw the "return 1".

    What I was trying to say is that the else part of the if statement, is completely unnecessary. The marriage of the two posts turned into something rather foobar, surely.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Its OK ADAK it happens just take a chill pill

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    If I use:

    Code:
    double data[100][6];
    And then in the body of the two for loops:
    Code:
        for(i = 0; i < nRow; i++) {
            for(j = 0; j < nCol; j++) {
                fscanf(fpIn, "%lf", &data[i][j]); 
            }       
        }     
        printf("file has been read in\n"); 
        printf("the first entry is:\n");
        printf("%lf\n", &data[0][0]);
    All that gets returned is 0.00000. I'm starting to get really fed up with C and this assignment.

    Apparently, I can do the same procedure with fgets and sscanf (our notes mention a spotty example of this). fgets pulls in each line essentially, but how would I read that to each row of 'data' using sscanf?

    Code:
    while(fgets(data, LINE_LENGTH, fpIn) != NULL)
    {
    sscanf(data, ????);
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why are you trying to print the address of data[0][0]? Why not print the actual value instead?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM