Confusion with fread from .txt array in C
Hey, I have been working on a couple of codes, one to create a matrix and save it, and one to open and display. It is to be used for matrix operations later on.
The array is created fine and saved in 2 parts, the array size in name.dat and the array components in name.txt, when I run the program, the first column of the array displays the size of the array and not the first index. The second and third etc, display their correct coefficient.
I am quite new to C and I suspect the error lies in my fread command. The code is meant to use as few headers as possible.
The code as it is:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<glob.h>
int main( void )
{
glob_t files_dat;
switch( glob("*.dat", 0, NULL, &files_dat) )
{
case 0:
break;
case GLOB_NOSPACE:
printf(" Out of memory\n");
break;
case GLOB_ABORTED:
printf("Reading Error\n");
break;
case GLOB_NOMATCH:
printf("No files found\n");
break;
default:
break;
}
printf("The following arrays were found:\n");
char file_txt[]="%s.txt";
char filename_txt[sizeof file_txt+10];
char name[20];
unsigned int i, j;
for(i=0; i<files_dat.gl_pathc; i++)
{
strcpy(name,files_dat.gl_pathv[i]);
for(j=0;j<strlen(name);j++){
if ((name[j]=='.') && (name[j+1]=='d' && (name[j+2]=='a' && (name[j+3]=='t')))){
strcpy(name + j, name + j+4);
strcpy(name + j+1, name + j+5);
strcpy(name + j+2, name + j+6);
strcpy(name + j+3, name + j+7);
}
}
sprintf(filename_txt,file_txt,name);
FILE *txt;
if((txt=fopen(filename_txt,"r"))!=NULL){
printf("%s\n", name );
}
fclose(txt);
}
char array_name[20];
char arr_txt[]="%s.txt";
char arr_dat[]="%s.dat";
char array_txt[sizeof arr_txt+10];
char array_dat[sizeof arr_dat+10];
FILE *farr_dat;
FILE *farr_txt;
printf("Enter array name to open:\n");
if( scanf( "%19s", array_name ) != 1){
printf("You must enter an array name!\n");
}
sprintf(array_txt,arr_txt,array_name);
sprintf(array_dat,arr_dat,array_name);
if((farr_dat=fopen(array_dat,"rb"))==NULL){
printf("File does not exist or read error.\n");
}
else if((farr_txt=fopen(array_txt,"rb"))==NULL){
printf("Read error from txt file.\n");
}
else {
printf("Opening %s:\n",array_dat);
int j, k, n;
while(fread(&n,1,sizeof(int),farr_dat)){
fseek(farr_dat, n, SEEK_CUR);
}
printf("n=%d.\n",n);
printf("Creating array %d x %d:\n",n,n);
double **my_array = malloc(n * sizeof(double *));
for(j=0;j<n;j++)
{
my_array[j] = malloc(n * sizeof(double));
}
printf("Array Created.\n");
for(j=0;j<n;j++)
{
*my_array[j]=fread(my_array[j],sizeof(double),n,farr_txt);
}
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
printf("[ %5f ] ", my_array[j][k]);
}
printf("\n");
}
}
return 0;
}
The output:
Code:
The following arrays were found:
Test_m1
twest
Enter array name to open:
twest
Opening twest.dat:
n=6.
Creating array 6 x 6:
Array Created.
[ 6.000000 ] [ 1.028571 ] [ 1.057143 ] [ 1.085714 ] [ 1.114286 ] [ 1.142857 ]
[ 6.000000 ] [ 1.200000 ] [ 1.228571 ] [ 1.257143 ] [ 1.285714 ] [ 1.314286 ]
[ 6.000000 ] [ 1.371429 ] [ 1.400000 ] [ 1.428571 ] [ 1.457143 ] [ 1.485714 ]
[ 6.000000 ] [ 1.542857 ] [ 1.571429 ] [ 1.600000 ] [ 1.628571 ] [ 1.657143 ]
[ 6.000000 ] [ 1.714286 ] [ 1.742857 ] [ 1.771429 ] [ 1.800000 ] [ 1.828571 ]
[ 6.000000 ] [ 1.885714 ] [ 1.914286 ] [ 1.942857 ] [ 1.971429 ] [ 2.000000 ]
Any advice or help is greatly appreciated.