How to use fscanf to read in data from a file into a multidimensional array

This is a discussion on How to use fscanf to read in data from a file into a multidimensional array within the C Programming forums, part of the General Programming Boards category; Hello, I'm working on a program that is meant to take a file formatted like: 100 200 300 10 20 ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    London
    Posts
    6

    How to use fscanf to read in data from a file into a multidimensional array

    Hello,
    I'm working on a program that is meant to take a file formatted like:

    100 200 300 10 20 30 5
    1 2 3 4 5 6 7
    12 23 34 45 56 67 78
    88 99 33 11 22 55 66
    98 87 76 65 54 43 32

    There a lot of rows (~10000) but right now I am just trying to write a program to read 5 rows..

    I need to read in the data into a 7 column x 5 row array and then print the array out. I can't seem to do that though, could someone take a look at the code and help me figure out what is wrong? I call the function "analyze_data" in the main function, but I can't get it to work. I'm not getting any syntax errors, but I'm not getting the output file called "Results" which I think I should be getting.

    Code:
    int analyze_data (FILE *fp)
    //reads in the data to an array, analyzes, and the prints the array
    
    {
    int j , i; 
    
    double data[7][5]; //declare array (random number)
    				 
    for(j = 0; j < 5; j++) //repeats for 5 rows
    {
    	for (i=0; i< 7; i++)
    		fscanf(fp, "%f%f%f%f%f%f", &data[i][j]);
    }
    		
    	
    //opens results file, prints results, closes file//
    			   FILE *fp2 = fopen ("Results", "w"); //opens up a file called "Results" and allows writing
    			 for(j = 0; j < 5; j++) //repeats for max number of columns
    			   {
    			   for (i=0; i< 7; i++)
    			   fprintf(fp2, "%f%f%f%f%f%f", data[i][j]);
    				}	
    				
    	int fclose(FILE *fp2);
    	return 0;
    	
    }
    Also, sorry if this is terrible code, I'm slightly new to this.
    Thank you!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You have 6 %fs in your format strings. Don't you want 1?

    int fclose(FILE *fp2); this is a function declaration. A declaration tells the compiler of something (contrasting to a definition which tells the compiler more of what that thing is). This does not invoke the function fclose, which you are probably intending to do here. Instead of listing fclose's declaration here, include stdio.h, which is supposed to have it already. Make sure to call fclose and close what you fopened.
    Last edited by robwhit; 11-28-2010 at 08:41 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    London
    Posts
    6

    fscanf still not working properly

    Thanks, I've fixed up my code. The fscanf function still isn't working right though, I get the right output format but all the values in the matrix are just 0.

    I think it's mainly just this part that there's something wrong with, is there something obvious?:

    Code:
    for(j = 0; j < 5; j++) //repeats for number of columns
      {
    	for (i=0; i< 7; i++)
    	  {
    	    fscanf(fp, "%f", &num);
    		  data[i][j] = num;
    	  }
       }
    Here is the whole code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    //when using gcc compiler you need to "gcc -lm main.c" in order to link it to the math library//
    
    // Opens the file and reads in the stream //
    void load_data(char fname[200])
    {
    double data[7][5];//declare array (random number)
    int j , i; 
     double num;
    
      FILE *fp = fopen(fname, "r");
      FILE *fp2 = fopen("/home/Desktop/Results", "w"); //opens up a file called "Results"
    			                                                   // and allows writing//
    	
    	if (fp == NULL) //checks for the file
    	{ printf("\n Canít open %s\n",fname);
    		exit;
    	}	
    
    //reads in the data to an array, analyzes, and the prints the array
     
    				 
    for(j = 0; j < 5; j++) //repeats for max number of columns
      {
    	for (i=0; i< 7; i++)
    	  {
    	    fscanf(fp, "%f", &num);
    		  data[i][j] = num;
    	  }
       }	
    
    for(j = 0; j < 5; j++) //repeats for max number of columns
      {
        for (i=0; i< 7; i++)
          { num = data[i][j];
    	fprintf(fp2, "%f ", num);
          }
        fprintf(fp2, "\n");
      }
    
    // add in laterz fprintf(fp2,"%f%f", radius, velocity);//
    				
    	fclose(fp2);
    	fclose(fp);
    }
    
    
    
    //Main Function //
    	
    	int main (void)
    	{
    		char path[200], basename[200], input_fname[200]; 
    		// Stores path & basename of file, input_fname: combines path and basename//
    		char str[50] = {0}; //stores filename
    		
    		printf("please input the file name: ");
    		scanf("%s", str); // gets input from user, %s reads characters, variable i//
    		 
    		sprintf(path, "/home/Desktop"); //Change pathway as needed //
    		snprintf(basename, 50, "%s", str); // stores basename from i//
    		sprintf (input_fname, "%s/%s", path, basename); //combines path and basename to get input_fname//
    		
    		printf("Using file: %s\n", input_fname); //tells user which file is being used
    
    		load_data(input_fname); //function to load input data //
    		
          			   
    					   
    		return 0;
    	}
    Thanks for the help.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    London
    Posts
    6
    Oh hey! I figured it out, I'm not supposed to use %f and declare the variable as a non-float type. Okay cool, thanks anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in C programming (too lazy)
    By cwillygs in forum C Programming
    Replies: 12
    Last Post: 04-20-2010, 12:23 AM
  2. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. Using fscanf for reading numerical data from text file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-15-2002, 05:18 PM

Tags for this Thread


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