fread & fseek not advancing buffer

This is a discussion on fread & fseek not advancing buffer within the C++ Programming forums, part of the General Programming Boards category; Neither fread nor fseek in my code advance the cursor position the file, whether I opened it as r or ...

  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
    Katy, Texas
    Posts
    2,309
    You say it doesn't work for you. How can you tell? What does it do for you?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 08:30 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04: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, 04:15 AM

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