fread cuts off last byte

This is a discussion on fread cuts off last byte within the C Programming forums, part of the General Programming Boards category; For some reason, when I look at the value of my buffer, its missing the last byte that was supposted ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    51

    fread cuts off last byte

    For some reason, when I look at the value of my buffer, its missing the last byte that was supposted to be copied by fread. Heres my code:

    Code:
            char * buff;
    	FILE *ptr;
    	long fileS;
    
    	ptr = fopen("myFile","r");
    
                    // The file size is 600 bytes
    	fileS = 600
    
    	buff = malloc(fileS);
    	fread(buffer,1,fileS,ptr);
    	fclose(ptr);
    Now, if I look at the memory contents of buff, everythings there, all the way from byte 1 to byte 599. Byte 600, was never copied. Any ideas?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by someprogr View Post
    Any ideas?
    Here are some ideas:

    • You don't know memory indexing starts at index 0 instead of index 1.
    • The file doesn't have 600 bytes.
    • All the bytes are in your buffer and you've imagined this problem.


    If you want a real explanation give us real information to go on.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > fileS = 600
    This lacks a ; so it won't compile.

    > buff = malloc(fileS);
    > fread(buffer,1,fileS,ptr);
    You allocate buff, yet read into buffer.

    Try posting what you actually tried, not random snippets recalled from memory of what you think the problem might be.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 04:19 AM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 04:46 PM
  5. sorting a structure of arrays
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 03-15-2002, 10:45 AM

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