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!
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.