Thread: fread & fseek not advancing buffer

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    fread & fseek not advancing buffer

    Neither fread nor fseek in my code advance the cursor position the file, whether I opened it as r or r+, and I (obviously) require the cursor to change to read past the first few bytes.

    Code:
    FILE* values;
    fopen_s(&values,"values.txt","rb");
    char bytes_read[2][4];
    
    for(int min=0;min<2;++min)
    {
    	// read the bytes for the maximum value of the hack
    	fread(bytes_read[min],2,1,values);
    	// advance the file
    	fseek(values,2,SEEK_CUR);
    }
    the 2s in fseek and fread are actually from another variable, but I substituted here for readability.

    This code doesn't work for me.

    Any help?

    Edit: Nevermind. The file I was attempting to read consisted of: 27 0F 00 01 27 0F 00 01 ... so when I read two bytes I got 27 0F.. then it advanced the buffer by 2.. however, the fseek advanced the buffer by an EXTRA 2, which brought me to the next 27 0F. sorry
    Last edited by RobotGymnast; 10-28-2008 at 04:38 PM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You say it doesn't work for you. How can you tell? What does it do for you?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    for(int min=0;min<2;++min)
    {
    	memset(bytes_read[min], 0, sizeof(bytes_read[min]));
    	// read the bytes for the maximum value of the hack
    	fread(bytes_read[min],2,1,values);
    	// advance the file
    	fseek(values,2,SEEK_CUR);
    }

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Also... why are you asking C code on this forum? You could use fstreams for this as well.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fread(bytes_read[min],2,1,values);
    1. bytes_read[i] is 4, not 2
    2. you ignore (at your peril) the return results of both these functions.

    Not to mention the fopen call as well.

    Remember, the _s is only safe if you actually have a clue.
    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.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I can't win... when I write code in a way that doesn't flat out give away an answer someone else just gives away an answer to the OP :-\ Anyway, hopefully you noticed when the memory was zeroed out that the garbage you were getting was because you were likely overwriting stuff that you were printing out.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    got it. repetetive file plus bad code. sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image Processing Help Required
    By dantu1985 in forum C Programming
    Replies: 7
    Last Post: 12-31-2007, 09:30 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM