fread problem with array

This is a discussion on fread problem with array within the C Programming forums, part of the General Programming Boards category; Hi there, I'm just trying to do easy load/save arrays from/to disk, and it seems to save fine, but fread ...

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

    fread problem with array

    Hi there,
    I'm just trying to do easy load/save arrays from/to disk, and it seems to save fine, but fread stops reading and the array becomes garbage, it cannot read more than 12 items!
    That's weird for me.
    The code below works fine for <12 items

    Code:
    // TEST FREAD-FWRITE.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "array values.h"
    #define NUMITEMS	50 
    
    
    int row=0;
    int  object_size = sizeof(double);
    int  object_count = NUMITEMS;
    int  op_return=0;
    FILE *my_stream;
    char my_filename[] = "my array.txt";
    
    
    void savearray(char *, int , int , void *);
    void loadarray(char *, int , int , void *);
    void printarray_double(double *);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//print initial values
    	printarray_double(my_array);
    
    	//save array values
    	savearray(my_filename, object_size, object_count, my_array);
    
    	//Erase initial values
    	for (int row = 0; row < NUMITEMS; row++)
    	{	
    		my_array[row]=0.0;
    	}
    
    	//Reload values for checking
    	loadarray(my_filename, object_size, object_count, my_array);
    
    	//print the loaded values
    	printarray_double(my_array);
    
    	system("pause");
    
    	return 0;
    }
    
    
    void loadarray(char *file_name, int object_size, int object_count, void *my_array)
    {
    	FILE *my_stream;
    	size_t op_return;
    	//open
    	my_stream = fopen (file_name, "r");
    	//read
    	op_return = fread (my_array, object_size, object_count, my_stream);
    	//close
    	fclose (my_stream);
    
    	//just for debugging
    	char	Screen_line[256];
    	sprintf ( Screen_line , "items read %d " , op_return );
    	strcat ( Screen_line, &file_name[0]);
    	printf ( "%s \n", Screen_line );    
    }
    
    void savearray(char *file_name, int object_size, int object_count, void *my_array)
    {
    	FILE *my_stream;
    	size_t op_return;
    	//open
    	my_stream = fopen (file_name, "w");
    	//write
    	op_return = fwrite (my_array, object_size, object_count, my_stream);
    	//close
    	fclose (my_stream);
    
    	//just for debugging
    	char	Screen_line[256];
    	sprintf ( Screen_line , "items written %d ," , op_return );
    	strcat ( Screen_line, &file_name[0]);
        printf ( "%s \n", Screen_line );
    }
    
    
    
    
    void printarray_double(double *my_array)
    {
    	char Screen_line[256];
    	for (row = 0; row < NUMITEMS; row++)
    	{	
    		sprintf ( Screen_line , "%f" , my_array[row] );
    		printf ( "%s %d \n", Screen_line, row );
    
    	}
    }
    And here are the array values that I'm trying. They are doubles, and there are 50 numbers, for testing purposes:

    Code:
    #define NUMITEMS	50
    
    double my_array[NUMITEMS] =
    {
       0.6118607142934980	,
       0.6117341610633070	,
       0.7314396627639230	,
       0.7504380032016010	,
       0.6524915987433790	,
       0.6053171425464650	,
       0.6137226827695900	,
       0.6298552566884320	,
       0.6299042083493930	,
       0.6975612239557440	,
       0.7358607679929620	,
       0.7677401627500350	,
       0.7661192296677770	,
       0.7691610452412540	,
       0.7723980442377540	,
       0.7099293417036730	,
       0.4851360877095980	,
       0.5965984585955460	,
       0.7449231672225140	,
       0.6114349730079800	,
       0.4537917812611730	,
       0.5476524688309480	,
       0.5230317118824670	,
       0.4583867380184910	,
       0.6895528340090980	,
       0.5227548315103280	,
       0.6524915987433790	,
       0.5140777936618300	,
       0.5433413334897790	,
       0.6235688561274040	,
       0.6995940599984630	,
       0.6004202253258840	,
       0.5531250052538760	,
       0.6882877552038610	,
       0.4629952579127150	,
       0.4737851050652420	,
       0.4579471848661540	,
       0.7007674750879090	,
       0.4069625093020240	,
       0.5792732686133030	,
       0.4167435889139460	,
       0.7053940665829620	,
       0.7071342058580180	,
       0.6231783960051730	,
       0.6076261525607750	,
       0.7173937684302550	,
       0.5584812837710740	,
       0.7035325317071100	,
       0.7035600943116600	,
       0.7358148066385620	   
      };
    This is a pain for me, because the real array that I want to write and load has around 10.000 items...

    Thanks in advance.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    I think maybe you should be using "rb" and "wb" since they are binary values. Also, maybe try using fflush() after your fwrite()

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Yes! 9132 items loaded here.
    That was it. I used fopen with "wb" and "rb".
    fflush was not neccessary.

    Thanks a lot, rockytriton, I was kind of lost.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  3. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM

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