Results 1 to 32 of 32

Thread: problem when reading from large binary file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241

    problem when reading from large binary file

    do you see anything wrong this this code. sSend is a class that encapsulates the winsock API.

    Code:
    ifstream openfile((const char *)FileName.c_str(),ios::in | ios::binary);
    	char tmpContent[10000] = {0};
    	int left = 0;
    	
    
    
    	if(openfile.is_open())
    	{
    		//int length = openfile.tellg();
    
    		while(!openfile.eof())
    		{
    			left = GetFileLen((char *)FileName.c_str()) - openfile.gcount();
    
    			if(left < 2048)
    			{
    				openfile.read(tmpContent, left);
    			}
    			else
    			{
    				openfile.read(tmpContent, 2048);
    			}
    
    			
    
    			//openfile.read(tmpContent, left);
    			
    			sSend.SendData(tmpContent);
    
    			
    		}
    
    		sSend.Close();
    	}
    Code:
    int GetFileLen(char *path)
    {
    	ifstream TheFile((const char *)path, ios::in | ios::ate | ios::binary);
    
    	return TheFile.tellg();
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You may get problems with file locking. Usually ifstream will lock the file so that it can't be opened another time.

    Why do you recalculate the file size every iteration? Calculate it once and be done with it.

    Why do you cast the return of c_str? It's already of type const char * and every useless cast is dangerous as it might occlude real errors.

    Why is the buffer 10000 bytes large when you never read more than 2048 bytes?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    ok i fixed the things u said the function now looks like this:

    Code:
    ifstream openfile(FileName.c_str(),ios::in | ios::binary);
    	char tmpContent[2048] = {0};
    	int left = 0;
    	int size = GetFileLen((char *)FileName.c_str());
    	
    
    
    	if(openfile.is_open())
    	{
    		//int length = openfile.tellg();
    
    		while(!openfile.eof())
    		{
    			left = size - openfile.gcount();
    
    			if(left < 2048)
    			{
    				openfile.read(tmpContent, left);
    			}
    			else
    			{
    				openfile.read(tmpContent, 2048);
    			}
    
    			
    
    			//openfile.read(tmpContent, left);
    			
    			sSend.SendData(tmpContent);
    
    			
    		}
    
    		sSend.Close();
    	}

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Looks ok. Does it work?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    unfortunately no, it worked fine for a small text file with about 15 characters in it. But then i tried it on a 24KB executable and it didn't work. It sent the first line of code correctly, but after that it just sent a couple long lines of weird looking ÿ's along with some other weird characters. The final file was only 4KB instead of 24KB. I have no clue why. It shouldn't matter whether tmpContent is a char or unsigned char right?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    by the way this function is a member of a class and takes place inside a thread.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You open it binary, is it supposed to contain printable data?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    no, but I opened its output in notepad.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then it's no surprise it's strange characters. So the only strange thing is that it doesn't copy the full amount of data. Hav you tried doing a debug run?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    it is a suprise that its strange characters since it is different strange characters then the original exe. I havent tried a debug run, but I did put a MessageBox before the part where I send the data and it shows the same strange characters that end up in the output file.

  11. #11
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    try

    char *tmpContent = new char[2048];

    strcpy(tmpContent, " ");


    Maybe the memory it allocated has garbage in it, and if its less than 2048, itll fill whatever amount, but leave the rest with garbage, that happens to me sometimes
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    the code is now this and it produces the same result with the same problem:

    Code:
    ifstream openfile(FileName.c_str(),ios::in | ios::binary);
    	char *tmpContent = new char[2048];
    	strcpy(tmpContent, " ");
    
    	int left = 0;
    	int size = GetFileLen((char *)FileName.c_str()) + 1;
    
    	if(openfile.is_open())
    	{
    
    		while(!openfile.eof())
    		{
    			left = size - openfile.gcount();
    
    			if(left < 2048)
    			{
    				openfile.read(tmpContent, left);
    			}
    			else
    			{
    				openfile.read(tmpContent, 2048);
    			}
    
    			MessageBox(NULL,tmpContent, "tmpContent", MB_OK);
    			
    			sSend.SendData(tmpContent);
    		}
    
    		sSend.Close();
    	}

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Your "SendData" function -- how does it know how big the data block is?

    Also, I'd recommend using memset() rather than strcpy() -- set all of tmpContent to 0 first.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    The code now looks like this and produces the same result with the same problem, I have commented the SendData function in case it was creating any problems.

    Code:
    	ifstream openfile(FileName.c_str(),ios::in | ios::binary);
    	char *tmpContent = new char[2048];
    	memset(tmpContent, 0, 2048);
    	int left = 0;
    	int size = GetFileLen((char *)FileName.c_str()) + 1;
    
    	if(openfile.is_open())
    	{
    
    		while(!openfile.eof())
    		{
    			left = size - openfile.gcount();
    
    			if(left < 2048)
    			{
    				openfile.read(tmpContent, left);
    			}
    			else
    			{
    				openfile.read(tmpContent, 2048);
    			}
    
    			MessageBox(NULL,tmpContent, "tmpContent", MB_OK);
    			
    			//sSend.SendData(tmpContent);
    		}
    
    		sSend.Close();
    	}

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't display binary data, remember.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    when u say display, do u mean MessageBox()?

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    ok i just made a big improvement. I fixed the SendData function so it works properly and i fixed a couple other things. Now the output file looks very similiar to the original file. They are both 24.0 KB in Size, but the output file is 28.0 size on disk (according to properties). What is the difference between size and size on disk? Also when I open up the original file and the output file in notepad they seem to have the same content, except in slightly different places. And all the weird characters are different in each file. Would it matter that im using char and not unsigned char?

  18. #18
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64
    Size on disk just means how much physical space it's taking. Your hard drive is divided into clusters, that are usually 8k big. Even a 1k file will take up 8k of space on your drive.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    So how could two files be the same size but different sizes on disk?

  20. #20
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It could be more fragmented.

    The cluster size is not always the same, it's dependent on the disk and on the file system.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    Is there any reason why it would be screwing up the binary characters? are you sure it doenst need to be an unsigned char?

  22. #22
    Lively Member
    Join Date
    May 2002
    Location
    Oregon
    Posts
    64
    Perhaps the problem is in the function that receives it?

  23. #23
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Hi, CornedBee, how "every useless cast is dangerous as it might occlude real errors"? Just wondering to know. As it might occur in my daily code

  24. #24
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Test Arawn's idea by directly writing to another file, then comparing those.

    Chris: The compiler disallows some implicit casts because it can't know if it makes sense, e.g. casting a base class pointer to a derived class pointer. Since the compiler can't know if the base class pointer really points to a derived object, and because it would cause huge problems if it didn't you have to explicitly cast the pointer, either with reinterpret_cast, dynamic_cast or C-style cast. Getting into the habit of explicitly casting everything that produces problems could for example lead to the following problems:
    1)
    Code:
    void func(int *some_val)
    {
      // lots and lots of code
      int i = some_val;
    }
    This creates a compiler casting error. Without really looking you just add the explicit cast.
    Code:
    void func(int *some_val)
    {
      // lots and lots of code
      int i = (int)some_val;
    }
    Bingo, you are *****ed.
    2)
    Code:
    #include <string>
    using std::string;
    
    string s("Hello there");
    // dumdidum
    // cast, else compiler error
    char * cs = (char *)s.c_str();
    If you now modify cs you have problems again. Casting away const is never good and means that something with your code design is wrong. The const modifier is really great.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  25. #25
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by CornedBee
    Code:
    #include <string>
    using std::string;
    
    string s("Hello there");
    // dumdidum
    // cast, else compiler error
    char * cs = (char *)s.c_str();
    If you now modify cs you have problems again. Casting away const is never good and means that something with your code design is wrong. The const modifier is really great.
    I don't think you're allowed that last one, I thought you needed const_cast It might have changed though.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You were allowed in C, because there was no const_cast, and I don't think they anyhow reduced the abilities of the C-style cast.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  27. #27
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    There wasn't originally a const in C, though.

    Just testing....
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  28. #28
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're right.
    Code:
    #include <iostream>
    
    void yarr(const int *blah) {
        int* hmmm = (int*)blah;
        *hmmm = 5;
    }
    
    int main() {
        int weebl;
        
        cout << weebl << endl;
        
        yarr(&weebl);
        
        cout << weebl << endl;
    }
    That compiles fine (and works, at least on this one...).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  29. #29
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    yarr(&weebl);
    LOLOLOLOL !!!!!
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  30. #30
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169


    Thought I'd add some insanity to the situation
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  31. #31

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    lol ok i figured out why it wasnt working. You were right the problem was on the receiving end. The program that was receiving it was made in VB and when I checked the source I found that it wasn't writing to the file in binary mode. I fixed that and now it seems to be working fine.

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    P.S. thx for all ur help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width