fread problems

This is a discussion on fread problems within the C Programming forums, part of the General Programming Boards category; Hello, I'm having problems with the fread function. I'm basically trying to read from a binary file. Here's my code ...

  1. #1
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625

    fread problems

    Hello, I'm having problems with the fread function. I'm basically trying to read from a binary file. Here's my code :

    Code:
    //  diskp is a pointer to the file to be read
    int read_blocks(int start_address, int nblocks, void *buffer)
    {
    	int f;
    	int err_num;
    
    	if ((fseek(diskp,start_address*BLOCK_SIZE,SEEK_SET)) != 0) // Seek to the point where read should start
    	{
    		fprintf(stderr,"Error in disk read : Unable to seek to given start location.\n");
    		return 0;
    	}
    
    	if ((f = (fread(buffer,BLOCK_SIZE,nblocks,diskp))) != nblocks)  // Read from disk to buffer
    	{
    		if(feof(diskp) != 0)
    		{
    			fprintf(stdout,"End of file reached. %d Blocks read.\n",f);
    			return 1;
    		}
    
    		if((err_num = ferror(diskp)) != 0)
    		{
    			fprintf(stderr,"%s\n",strerror(err_num));
    		}
    
    		fprintf(stderr,"Error in disk read : Buffer write failed. %d\n",f);
    		return 0;
    	}
    	
    	return 1;
    }
    Ok. So everytime I run this. I get an error saying "Operation not permitted", and 0 blocks are read. At first my intuition said that maybe I didn't have read permission on the file I was working on, but an ls, latter showed that :

    Code:
    total 44
    -rwxrwxrwx  1 ythiel  16268   200 Mar 14 12:24 disk  <--- file I am working on
    -rwxr-xr-x  1 ythiel  16268  8136 Mar 14 12:24 disk_emu
    -rw-------  1 ythiel  16268  3966 Mar 14 12:24 disk_emu.c
    -rw-------  1 ythiel  16268  3968 Mar 14 12:23 disk_emu.c~
    -rw-------  1 ythiel  16268   251 Mar 10 12:47 disk_emu.h
    -rw-------  1 ythiel  16268  7323 Mar 10 12:47 disk_test.c
    -rwx------  1 ythiel  16268  8256 Mar 13 17:45 diskemu
    -rw-r--r--  1 ythiel  16268     0 Mar 14 12:36 dud
    So I don't think that's the problem. Any ideas ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    > fread(buffer,BLOCK_SIZE,nblocks,diskp
    Is your buffer really pointing at BLOCK_SIZE * nblocks bytes?

    Normally, if you just want to read 'n' bytes, you just do
    bytesRead = fread(buffer,1,nbytes,diskp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I believe it is, here's how it's initialised :

    Code:
    data = (int*)calloc(1,(5*BLOCK_SIZE));
    I then call my function using this :

    Code:
    read_blocks(10,5,data);
    now the file I am working on is 80 bytes long, seperated into 20 blocks of 4 bytes (actually, sizeof(int) bytes), so even if those blocks are empty (which I don't think they are, anyways), I don't see how I would be getting an error.

    Also, I actually do in fact have to seperate it into blocks of a certain given size. And although I can see how I could could just as well do this :

    Code:
    fread(buffer,1,BLOCK_SIZE*nblocks,diskp)
    When I tried that, I got the same error.

    Any other ideas ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    You'd have to post more code - like how some of those variables and constants are declared in main()

    I take it diskp is a FILE* opened for reading binary data?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I feel so stupid. As it turns out, after discussing this with my TA this morning, I was opening my file with only write permission. Adding a little '+' to the whole thing solved the problem. Thank you for your time Salem.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Reading from binary file; fread problems?
    By pmgeahan in forum C Programming
    Replies: 3
    Last Post: 01-15-2009, 04:07 PM
  3. fread problems
    By firyace in forum C Programming
    Replies: 19
    Last Post: 06-14-2007, 04:09 PM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 08:08 AM

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