Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
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.
 
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.
 
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
 
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.
 
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
 
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);

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 );
}
 
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.