readding an array from a file.

Discussion in 'Mac Programming' started by farmerdoug, Jan 13, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    The an array was read into a file as follows:
    Code:
     fp = fopen(filename, "w");
    	
    	for ( i = 0; i < 1500; i++)
    		for(j = 0; j < 5; j++)
    			for (k = 0; k < 45; k++)
    				
    					fwrite((char *)Paverages[i][j][k], sizeof(char), 8, fp);			
    	fclose(fp);
    
    Should I be able to read it back out in the same way just using fread. When I try it I get that no bytes have been read.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post your code that uses fread. Extrapolating what your code looks like means we have to guess what code you wrote.

    Also please explain exactly how you know that no bytes have been read.

    Also, please show the declaration of the array being read into. You've left out the variable declarations in several posts now, but it's impossible to correctly interpret your code without seeing the declaration of the variable.

    Also note that both fread() and fwrite() returns counts indicating how much was read or written, respectively. If you ignore this value, and also ignore ferror() and feof(), there is no assurance that what you asked for was actually done.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    You can start by looking at your file's contents with od -ta. If the right contents are there, it should be possible to read it back out. As chown33 (and others in other threads) said, post complete, compilable code and we'll help. Otherwise we're just guessing.

    -Lee
     
  4. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #4
    reading an array

    The code for reading is exactly the same. Actually a little enhance from my earlier post.
    Code:
    	if ((fp = fopen(filename,"r")) == NULL)
    		printf(" File not opened\n");
    
    	
    	for ( i = 0; i < 1500; i++)
    		for(j = 0; j < 5; j++)
    			for (k = 0; k < 45; k++)
    				{
    				n = fread((char*)Pminus1[i][j][k], sizeof(char), 8, fp);
    					printf("%d\n", ferror(fp));
    						   }
    	fclose(fp);
    
    Some extra hints I have looked at the file in text edit and there are lots of chars there to read.
    When I print out n the number of bytes read, I get zero but ferror also returns O.
    Also I have checked that the array Pminus1 has memory allocated. I can write to it and print it out.
    The code fails whether or not the (char *) is in front of Pminus1.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    If this isn't working for you, there's something else going on. There's nothing specific wrong with the small amount of your code you posted (there's no need to cast something that's already a char * to a char *, but it's not really a problem). That means there is a problem somewhere else. If n is always 0, the file you're opening to read is empty, etc.

    I added the fwrites and freads, fopens, etc. to existing code i wrote on this same topic earlier and it worked fine. Use od -ta to make sure the contents of the file are correct. If so, the issue is with reading. Is filename definitely correct? It should be very easy to write a small test program to read the file, and simply print the values being read up by reading into a temporary variable without all of the array business. If that works, than your array may be the issue.

    -Lee
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    If fopen() returns null, your code will print a message but then proceed into the nested loops, which will attempt to fread() with a null value of fp. That won't work.

    When fopen() fails, it must halt further processing, using whatever exit strategy is appropriate for the surrounding context. That might be returning from the enclosing function, calling exit(), calling abort(), etc.

    Code:
    if ((fp = fopen(filename,"r")) == NULL)
    {
      perror(filename);
      exit(1);  // must not proceed
    }
    
    for ...
    
    Please see the man page for perror().


    EDIT: furead.c

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    static char cx4[1500][5][45][8];
    
    static long
    furead( const char * filename )
    {
    	long count = 0;
    	
    	FILE * fp;
    
    	if ((fp = fopen(filename,"r")) == NULL)
    	{
    		perror( filename );
    		return -1;
    	}
    
    	int i, j, k, n = 0;
    	for ( i = 0; i < 1500; i++)
    		for(j = 0; j < 5; j++)
    			for (k = 0; k < 45; k++)
    				{
    				n = fread((char*)cx4[i][j][k], sizeof(char), 8, fp);
    				if ( n <= 0 )
    					return count;
    
    				count += n;
    
    				printf("%d %d\n", n, ferror(fp));
    				 }
    	fclose(fp);
    
    	return count;
    }
    
    int main (int argc, const char * argv[]) 
    {
    	printf( "Starting furead...\n" );
    
    	if ( argc < 2 )
    	{
    		printf( "usage: cmd file\n" );
    		exit( 1 );
    	}
    
    	long count = furead( argv[ 1 ] );
    	printf( " count: %ld\n", count );
    }
    
     
  7. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #7
    reading an array

    I wrote 10 files and checked one with text edit. However, the one I tried to open in my code was a different file which was empty.

    You guys rock. :)
     

Share This Page