Thread: Getting size read with ifstream and read()

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Getting size read with ifstream and read()

    Hey guys,

    I have the following function that I use to send binary data over the network:
    PHP Code:
    bool CONNECTION::SendBinary()
    {
        
    char Buffer[10000];

                                                                    
    // Open the file as binary
        
    ifstream hFile (RealFile.c_str(), ios::binary);
        while (!
    hFile.eof())                                        // Keep reading it in
        
    {
            
    hFile.read(Buffer10000);
            
    int Y send(SFDBuffer10000);                        // Send data as we read it
        
    }
        
    hFile.close();                                                // Close
        
    return true;

    Now, the problem with it is when it gets to the end of the file it tries to read in 10000 bytes (I think), but there aren't that many, and it ends up sending junk at the end. I need a way that I can see exactly how many bytes were read in so I can send.

    So all I really want to know is, how can I check how many bytes an ifstream'd file read()'s. Thanks for any help

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try the readsome() function. It works the same way as read(), but it returns the number of characters read:

    streamsize readsome(char* pstr, streamsize n);

    When fewer than n characters are available, the function sets the flag ios::eofbit causing the eof() function to return true--so your while statement will work normally.
    Last edited by 7stud; 08-03-2003 at 12:36 AM.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Okay, I changed it to readsome and I now have this:
    PHP Code:
    ifstream hFile (RealFile.c_str(), ios::binary);
        while (!
    hFile.eof())                                        // Keep reading it in
        
    {
            
    int X hFile.readsome(Buffer10000);
            
    int Y send(SFDBufferX0);                        // Send data as we read it
        
    }
        
    hFile.close();                                                // Close
        
    return true
    But now its not even sending anything at all I think I haven't quite understood what you meant, have I done it right?

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Thanks Salem, but that still did not work This is what I have now:
    PHP Code:
    bool CONNECTION::SendBinary()
    {
        
    char Buffer[10000];

                                                                    
    // Open the file as binary
        
    ifstream hFile (RealFile.c_str(), ios::binary);
        
    int X 0;
        while ( (
    hFile.readsome(Buffersizeof(Buffer)) ) > )
        {
            
    send(SFDBufferX0);                                // Send data as we read it
        
    }
        
    hFile.close();                                                // Close
        
    return true;

    But its not sending anything still :S Yet when I use the read() function I used to have, it sent everything (but then a load of crap at the end when it reached the end of the file).

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Strangest thing...

    Okay the wierdest thing is happening to me. I made the following code to test readsome:

    PHP Code:
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    #include <string>

    using namespace std;

    int main()
    {
        
    char Buffer[10000];
        
    char RealFile "C:\\SWS\\WebRoot\\SWS.jpg";             
                        
        
    ifstream hFile (RealFileios::binary);
        
    int X 1;
        while (
    0)
        {
            
    int X hFile.readsome(Buffer1000);
            
    cout << "Read " << << endl;                              
        }
        
    hFile.close();                                               
        return 
    true;

    My thinking is that when readsome reads nothing, it will end the loop and stop the program. The file DOES exist I've checked so theres nothing wrong there.

    When I ran the program it printed "Read 0"... over and over again That makes no sense, it should only print it once and then end the program. But no, it prints it over and over. In fact thats all it prints, at the start it doesn't seem to read anything.

    All I can think of is that its printing the Ansi "0", not the number 0. So I made it:
    cout << "Read " << X * 2 << endl;
    That way it will double the ansi value and make a new number... or else it will still be 0 (0 * 2 = 0).

    I don't get why this is happening Maybe my computer is broken. Darn visual c++, damn it to hell.

    Edit: BTW, I do have double slashes in the file name, but the vBulletin must have taken them back to one.
    Last edited by nickname_changed; 08-03-2003 at 01:47 AM.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah I knew that was the problem, but I'm stuck because I don't know how to find how many bytes read returned. I'd never heard of readsome 'till today.

    Do you know any other ways I could count how many bytes were read? Or an alternative way to read the binary data?

    Also,I'm sorry about the PHP tags, I always found them much easier to read because the colors are different, but I wont use them from now on.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This is what you did:
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int x = 1;
    	while(x > 0)
    	{
    		int x = 0;
    		cout<<"hello\n";
    	}
    
    	return 0;
    }
    Run that and see what happens.

    read() returns an istream&.

    Here is a description of readsome() with some example code:

    http://www.cplusplus.com/ref/iostrea.../readsome.html
    Last edited by 7stud; 08-03-2003 at 02:58 AM.

  8. #8
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    The function returns an integer value of type streamsize representing the number of characters successfully read.
    What I did was set X to the value read So that each time I would know how much was read, I dont see how thats the same as setting it to 0. And that still doesn't explain what was happening with the loop I did because it wasn't like yours.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "That makes no sense, it should only print it once and then end the program. But no, it prints it over and over."

    My example was a demonstration of why you're code causes an infinite loop--what you assign x within the loop is irrelevant.

    I can't get readsome() to return a value either.
    Last edited by 7stud; 08-03-2003 at 04:32 AM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    bool CONNECTION::SendBinary()
    {
        char Buffer[10000];
    
                                                                    // Open the file as binary
        ifstream hFile (RealFile.c_str(), ios::binary);
        while (!hFile.eof())                                        // Keep reading it in
        {
            hFile.read(Buffer, 10000);
            int Y = send(SFD, Buffer, hFile.gcount(), 0);        // Send data as we read it
        }
        hFile.close();                                                // Close
        return true;
    }

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or:
    Code:
    bool CONNECTION::SendBinary()
    {
        char Buffer[10000];
    
                                                                    // Open the file as binary
        ifstream hFile (RealFile.c_str(), ios::binary);
        while (hFile.read(Buffer, 10000))                           // Keep reading it in
        {
            int Y = send(SFD, Buffer, hFile.gcount(), 0);            // Send data as we read it
        }
        int Y = send(SFD, Buffer, hFile.gcount(), 0);            // Send data as we read it
        hFile.close();                                                // Close
        return true;
    }
    Last edited by swoopy; 08-03-2003 at 05:31 AM.

  12. #12
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    OMG

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int x = 1;
    	while(x > 0)
    	{
    		int x = 0;
    		cout<<"hello\n";
    	}
    
    	return 0;
    }
    I looked at that and didn't bother to compile it because I thought it would print hello once, and then exit the loop. How is this happening?! This goes against everything I thought I knew about programming. This is how I looked at the loop.

    First it enters the loop with X bieng 1. 1 is greater than 0, so it sets X to 0 and says hello. Then, it starts again. This time 0 is NOT greater than 0, it is in fact equal. So the loop ends.

    But if that code compiles, this is what it says (when I read it):
    First it enters the loop with X bieng 1. 1 is greater than 0, so it sets X to 0 and says hello. Then, it starts again. This time 0 is greater than 0 (impossible) and so X is set to 0 again and hello is printed again. This happens all the time.

    THIS DOES NOT MAKE SENSE!!! WHERE HAVE I GONE WRONG OVER THE LAST 2 YEARS!

    Today has not been a good day WTF I dont get why this is happening. Can someone be kind enough to show a dumb kid whats really happening?

  13. #13
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    L - O - L

    Ohh I just read that again. I now see that I have 'int X' twice, and so its declared in a diferent set of paranthesis and so they're different values. I understand now. Boy, for a second there I was worried. Now I know where I've gone wrong. Thanks very much!

    Gosh I'm so darn stupid lol

  14. #14
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Okay thanks very much guys for sorting all this out. The code is working fine thanks to what swoopy gave and thanks to 7stud. lol, 7stud you really had me worried with that code, I thought I had lost my marbles or something lol, never do that to me again. But thanks, you helped me understand the error of my ways.

    As usual you've all been a great help I can't thank you enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question] ifstream read file, thanks!
    By userpingz in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2009, 06:38 PM
  2. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  3. Read problems
    By xddxogm3 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2003, 12:17 AM
  4. Help with ifstream read plz
    By dp_goose in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:35 AM
  5. why can't ifstream read spaces or enters
    By bobish in forum C++ Programming
    Replies: 11
    Last Post: 06-13-2002, 04:23 PM