Thread: Subtle(?) File I/O Problem

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Subtle(?) File I/O Problem

    Hi All,

    I've got another sanity-questioning problem to pose for you. I am trying to read the contents of a file, but I'm only able to read a small fraction of the file (15232 / 2912998 Bytes). gfc->mfbuf_p is a file pointer that was just recently writing to this same file. To be safe, I close this file and reopen it for reading only with a new file pointer. The code is given below.

    Code:
    void func(...) {
    
    	FILE *fp;
    	int sz;
    	struct flags *gfc;
    	sample_t mfbuf[2][MFSIZE];  // MFSIZE is 3984
    
    	...
    
    	// Open the temporary file for storing sampled data...
    	gfc->mfbuf_p = fopen(TEMP_FILE, "w+");
    	if(gfc->mfbuf_p == NULL) {
    		printf("Could not open temporary file!\n");
    		exit(1);
    	}
    	
    	
    	...
    	
    	// Fill up a large mfbuf
    	sz = fwrite((void*) mfbuf[0], sizeof(sample_t), mf_needed, gfc->mfbuf_p);
    	sz += fwrite((void*) mfbuf[1], sizeof(sample_t), mf_needed, gfc->mfbuf_p);
    
    	if (sz != 2*mf_needed) {
    		printf("FWRITE failed!  Only %d bytes were written, not %d bytes", sz, 2*mf_needed);
    		exit(1);
    	}
    	
    	...
    	
    	// Read saved samples from file and encode them...
    	//	Had some problems reading.. let's close and reopen
    	if(fflush(gfc->mfbuf_p) != 0) {
    		perror("FFLUSH failed!");
    		exit(1);
    	}
    
    	if (fclose(gfc->mfbuf_p) != 0) {
    		perror("Closing failed!");
    		exit(1);
    	}
    
    	fp = fopen(TEMP_FILE, "r");
    
    	if (fp == NULL) {
    		perror("Unable to open tmp file for reading!\n");
    		exit(1);
    	}
    
    	while (!feof(fp)) {
    		
    		int mf_needed, sz;
    		long off;
    
    
    		off = ftell(fp);
    		printf("\n\nOFfset: %ld\n", off);
    
    		mf_needed = 1904;
    
    		sz = fread((void*) &mfbuf[0][0], sizeof(sample_t), mf_needed, fp);
    
    		off = ftell(fp);
    		printf("\n\nOFfset: %ld\n", off);
    
    		if(sz != mf_needed  && !feof(fp)) {
    			perror("\n\nFAILED to read from temp file ONE!\n\n");
    			exit(1);
    		}
    		
    		sz += fread((void*) &mfbuf[1][0], sizeof(sample_t), mf_needed, fp);
    
    		if(sz != 2*mf_needed  && !feof(fp)) {
    			perror("\n\nFAILED to read from temp file!\n\n");
    			exit(1);
    		}
    	}
    	
    }

    This seems to work the first iteration, but fails the second iteration. It says it reads 1377 Bytes (out of the 1904 it should have read), and errno indicates an invalid argument. As I step through, I can see that the file pointer itself still points to a valid FILE structure, though after the failing read, I suspect some of its internal pointers may be corrupt (but I have no way of knowing).

    Any ideas?
    Last edited by cecomp64; 07-16-2008 at 10:55 AM. Reason: typo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  3. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM