Thread: fread cuts off last byte

  1. #1
    Registered User
    Join Date
    Dec 2007

    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:

            char * buff;
    	FILE *ptr;
    	long fileS;
    	ptr = fopen("myFile","r");
                    // The file size is 600 bytes
    	fileS = 600
    	buff = malloc(fileS);
    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
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

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
Website Security Test