Confusion with fread from .txt array in C

This is a discussion on Confusion with fread from .txt array in C within the C Programming forums, part of the General Programming Boards category; Hey, I have been working on a couple of codes, one to create a matrix and save it, and one ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    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.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    dm14v07,

    Code:
         1	#include<stdio.h>
         2	#include<stdlib.h>
         3	#include<string.h>
         4	#include<glob.h>
         5	
         6	int main( void )
         7	{
         8		glob_t files_dat;
         9		
        10		switch( glob("*.dat", 0, NULL, &files_dat) )
        11		{
        12			case 0:
        13				break;
        14			case GLOB_NOSPACE:
        15				printf(" Out of memory\n");
        16				break;
        17			case GLOB_ABORTED:
        18				printf("Reading Error\n");
        19				break;
        20			case GLOB_NOMATCH:
        21				printf("No files found\n");
        22				break;
        23			default:		
        24				break;
        25		}
        26		printf("The following arrays were found:\n");
        27		char file_txt[]="%s.txt";
        28		char filename_txt[sizeof file_txt+10];
        29		char name[20];
        30		unsigned int i, j;
        31		for(i=0; i<files_dat.gl_pathc; i++)
        32		{
        33			
        34			strcpy(name,files_dat.gl_pathv[i]);
        35			for(j=0;j<strlen(name);j++){
        36				if ((name[j]=='.') && (name[j+1]=='d' && (name[j+2]=='a' && (name[j+3]=='t')))){
        37				strcpy(name + j, name + j+4);
        38				strcpy(name + j+1, name + j+5);
        39				strcpy(name + j+2, name + j+6);
        40				strcpy(name + j+3, name + j+7);
        41				}
        42			}
        43			sprintf(filename_txt,file_txt,name);
        44			FILE *txt;
        45			if((txt=fopen(filename_txt,"r"))!=NULL){
        46				printf("%s\n", name );
        47			}
        48			fclose(txt);
        49		}
        50	
        51		char array_name[20];
        52		char arr_txt[]="%s.txt";
        53		char arr_dat[]="%s.dat";
        54		
        55		char array_txt[sizeof arr_txt+10];
        56		char array_dat[sizeof arr_dat+10];
        57		FILE *farr_dat;
        58		FILE *farr_txt;
        59		printf("Enter array name to open:\n");
        60		if( scanf( "%19s", array_name ) != 1){
        61			printf("You must enter an array name!\n");
        62		}
        63		sprintf(array_txt,arr_txt,array_name);
        64		sprintf(array_dat,arr_dat,array_name);
        65		if((farr_dat=fopen(array_dat,"rb"))==NULL){
        66			printf("File does not exist or read error.\n");
        67		}
        68		else if((farr_txt=fopen(array_txt,"rb"))==NULL){
        69			printf("Read error from txt file.\n");
        70		}
        71		else {
        72			printf("Opening %s:\n",array_dat);
        73			int j, k, n;
        74			while(fread(&n,1,sizeof(int),farr_dat)){
        75			fseek(farr_dat, n, SEEK_CUR);
        76			}
        77			printf("n=%d.\n",n);
        78			printf("Creating array %d x %d:\n",n,n);
        79			double **my_array = malloc(n * sizeof(double *));
        80			for(j=0;j<n;j++)
        81			{
        82				my_array[j] = malloc(n * sizeof(double));
        83			}
        84			printf("Array Created.\n");
        85			for(j=0;j<n;j++)
        86			{
        87				*my_array[j]=fread(my_array[j],sizeof(double),n,farr_txt);
        88			}		
        89			for(j=0;j<n;j++)
        90			{
        91				for(k=0;k<n;k++)
        92				{
        93					printf("[ %5f ] ", my_array[j][k]);
        94				}
        95				printf("\n");
        96			}
        97		}
        98		return 0;
        99	}
    This might help with such a large block...I used 'cat -n' to print the code with some line numbers first.

    The lines 85-8 are indeed the trouble. Here is a link to the 'fread' man page: FREAD. Fread has returned the number of elements it read. Upon returning, that value is assigned to the first element of each row...C being row major. Rather than line 87, try something like...

    Code:
            if( n != fread( my_array[j], sizeof(double), n, farr_txt) )
            {
                fprintf( stderr, "There's a problem with the array data...\n");
            }
    At line 28, I couldn't believe it even compiled.... Then I thought about it, and realized that the length of a constant string is a constant...thus the pre-processor could have gotten that one. Just seemed like a terrible thing to ask of it though. I don't like how the declarations kind of happen some where along the program flow either, but that's just a habit I've got I guess.

    I've still got the code in front of me, and I'll continue reading it now.

    Best Regards,
    Last edited by new_ink2001; 05-01-2011 at 11:13 PM. Reason: syntax
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    new_ink2001

    Thank you very much for your help. that link helped me understand how fread works, and returns the length of the array. Spent a long time trying to understand what I was doing wrong. only to realise that I was using fread to write over the array I was reading. As I said, I am quite new at this, especially FILE I/O.

    Here is the temporary fix:
    Code:
    int temp;
    for(j=0;j<n;j++)
    		{
    			array_b=fread(my_array[j],sizeof(double),n,farr_txt);
    		}
    not very stable, but its a start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with fread into array
    By doia in forum C Programming
    Replies: 4
    Last Post: 03-22-2010, 01:53 PM
  2. fread not playing nice; my array is full of zeros
    By voraciousveggie in forum C Programming
    Replies: 14
    Last Post: 03-16-2010, 10:36 PM
  3. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 02:45 PM
  4. fread file contents in to 2D Array!
    By strokebow in forum C Programming
    Replies: 12
    Last Post: 12-01-2006, 05:17 AM
  5. fread problem with array
    By MikeMorcilla in forum C Programming
    Replies: 2
    Last Post: 10-19-2005, 12:26 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