fread problem

This is a discussion on fread problem within the C Programming forums, part of the General Programming Boards category; Hi I am having problem reading file using fread. I was looking at emule code in which at certain point ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    fread problem

    Hi
    I am having problem reading file using fread.
    I was looking at emule code in which at certain point it read a file server.met
    using fread.
    I copied that code and build simple VC++ application to see its effect.
    When ever i run this code in VC++6 the 1) displays 24 which is right but
    2) displays 16908288, may be garbage value (It should be 12 as when i
    compile and run actual application 4m which i copied it diplays 24 and then 12).

    I ran the code using Borland c ver 3 and it diplays the correct value 24 and
    and then 12.

    So why i am getting 24 and then 16908288(instead of 12)
    The file server.met can be downloded 4m
    http://www.zshare.net/download/server-met.html
    Code:
                   struct ServerMet_Struct {
    	unsigned char	ip[4];
    	unsigned short	port;
    	unsigned int tagcount;
                    };
    
        	FILE* servermet = fopen("server.met", "rb");
    	
                     unsigned int fservercount;
    	
    	fread(&fservercount,4,1,servermet);
                    char buff[10];
    	itoa(fservercount,buff,10);
      1)           AfxMessageBox(buff);     //or cout<<buff;
                  
    
    	ServerMet_Struct sbuffer;	
    	
    	fread(&sbuffer,1,sizeof(ServerMet_Struct),servermet);
    	
    	
    	itoa(sbuffer.tagcount,buff,10);
      2)	AfxMessageBox(buff);  // or cout<<buff;

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    check out the actual parameters of fread.

    Code:
    fread(&sbuffer,1,sizeof(ServerMet_Struct),servermet);
    ssharish2005

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    The code is 100% same 4m actual coder. I have double checked it.
    thanks.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Your server.met does not contain PG-13 type material -- don't do that.

    Are you aware that structures may be padded?
    http://c-faq.com/struct/padding.html
    http://msdn.microsoft.com/library/de...vcrefAlign.asp
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Thanks very much

    Thanks very much

    I was dissappointed first and start checking project setting where
    i find compiler options. My one was not using /Zp1 /wd4200
    switches which i think were need for alignment and padding of
    structures.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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