fread problem

This is a discussion on fread problem within the C Programming forums, part of the General Programming Boards category; im trying to load a file into a memory buffer then display what's in the buffer. Im getting the right ...

  1. #1
    win
    win is offline
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    fread problem

    im trying to load a file into a memory buffer then display what's in the buffer. Im getting the right data but some of the bytes are padded with FFs. When looking at the bytes in decimal, i got alot of negative numbers. Am I doing something wrong? I tried opening the same file in a hex editor and it was fine, so it's not my file that's wrong, its something in my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main()
    {
    	
    	FILE *myfile;
    	long len;
    	char *buf;
    	int i;
    
    myfile=fopen("PONG","rb");
    if (myfile==NULL){
    printf("error opening file");
    exit(1);}
    else
    fseek(myfile,0,SEEK_END); 
    len=ftell(myfile); 
    fseek(myfile,0,SEEK_SET); 
    buf=(char *)malloc(len); 
    fread(buf,len,1,myfile); 
    fclose(myfile);
    
    
    for(i=0;i<len;i++)
    printf("%x ", *(buf+i));
    
    free(buf);
    
    
    
    
    	return 0;
    }
    and here is my output

    Code:
    6a 2 6b c 6c 3f 6d c ffffffa2 ffffffea ffffffda ffffffb6 ffffffdc ffffffd6 6e 0
    22 ffffffd4 66 3 68 2 60 60 fffffff0 15 fffffff0 7 30 0 12 1a ffffffc7 17 77 8 6
    9 ffffffff ffffffa2 fffffff0 ffffffd6 71 ffffffa2 ffffffea ffffffda ffffffb6 fff
    fffdc ffffffd6 60 1 ffffffe0 ffffffa1 7b fffffffe 60 4 ffffffe0 ffffffa1 7b 2 60
     1f ffffff8b 2 ffffffda ffffffb6 60 c ffffffe0 ffffffa1 7d fffffffe 60 d ffffffe
    0 ffffffa1 7d 2 60 1f ffffff8d 2 ffffffdc ffffffd6 ffffffa2 fffffff0 ffffffd6 71
     ffffff86 ffffff84 ffffff87 ffffff94 60 3f ffffff86 2 61 1f ffffff87 12 46 2 12
    78 46 3f 12 ffffff82 47 1f 69 ffffffff 47 0 69 1 ffffffd6 71 12 2a 68 2 63 1 fff
    fff80 70 ffffff80 ffffffb5 12 ffffff8a 68 fffffffe 63 a ffffff80 70 ffffff80 fff
    fffd5 3f 1 12 ffffffa2 61 2 ffffff80 15 3f 1 12 ffffffba ffffff80 15 3f 1 12 fff
    fffc8 ffffff80 15 3f 1 12 ffffffc2 60 20 fffffff0 18 22 ffffffd4 ffffff8e 34 22
    ffffffd4 66 3e 33 1 66 3 68 fffffffe 33 1 68 2 12 16 79 ffffffff 49 fffffffe 69
    ffffffff 12 ffffffc8 79 1 49 2 69 1 60 4 fffffff0 18 76 1 46 40 76 fffffffe 12 6
    c ffffffa2 fffffff2 fffffffe 33 fffffff2 65 fffffff1 29 64 14 65 0 ffffffd4 55 7
    4 15 fffffff2 29 ffffffd4 55 0 ffffffee ffffff80 ffffff80 ffffff80 ffffff80 ffff
    ff80 ffffff80 ffffff80 0 0 0 0 0 Press any key to continue . . .
    Ive attached the file that I am reading into memory. Just remove the txt extension as it is not a txt file but a binary file.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    Type char is a signed value by default with most compilers.
    Use type unsigned char instead.

  3. #3
    win
    win is offline
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by KenJackson View Post
    Type char is a signed value by default with most compilers.
    Use type unsigned char instead.
    thank you. it worked.

    any easy way to pad a 0 onto the byte so it shows 2 characters instead of 1. instead of just outputting lets say 2 it will output 02???

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by win View Post
    thank you. it worked.

    any easy way to pad a 0 onto the byte so it shows 2 characters instead of 1. instead of just outputting lets say 2 it will output 02???
    Yes:
    Code:
    scanf("%02x")
    '0' is the character to use as a pad, 2 is the number of digits total in the output.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    win
    win is offline
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by MK27 View Post
    Yes:
    Code:
    scanf("%02x")
    '0' is the character to use as a pad, 2 is the number of digits total in the output.
    thanks guys.got everything going fine

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problem
    By coolsmarty in forum C Programming
    Replies: 4
    Last Post: 01-13-2007, 01:43 PM
  2. Fread type problem
    By conor20_ie in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2006, 04:24 AM
  3. return value of fread()
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-24-2006, 06:31 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. model load problem
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 01-05-2006, 05:33 PM

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