Thread: Proper method of clearing buffer

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252

    Proper method of clearing buffer

    Hido, ho. Simple question, but one that's been buggin' me. Below is just a snipet and it compiles (though I get a warning about deleting the buffer). So, the question, what's the proper method of clearing the buffer (as it's used several times).

    Code:
           ifstream fin(DRevrNote.c_str(),ios::in);
    
             if (fin.good())
    
               {
                    fin.seekg( 0, ios::end ) ;
                    int size = fin.tellg() ;
                    fin.seekg( 0, ios::beg ) ;
    
                    char tempRead [size + 1];
                    fin.read( tempRead, size + 1) ;
    
                    tempRead[ size + 1 ] = 0 ;
    
                    fin.close();
    
                    delete []tempRead; //warning here...
               }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Technically, that code is illegal in C++. It probably compiles because you are using gcc which allows it as a non-standard extension (it is legal in C). The part that is illegal is the array that has a dynamic size that isn't known until runtime. In C++ you would normally use a vector<char> (an array allocated with new[] is another option).

    If you stick with the array as you have it, then you should not be calling delete[] since the array was not allocated with new[]. The array is locally declared, and it will be destroyed automatically when control reaches the end of the if block. (If you switched to vector it would also be automatically destroyed).

    Note that it is not "cleared", the memory is just freed for later use by the program. If you really wanted to clear the buffer by setting all bytes to 0, then you would use memcpy.

    A couple other changes you might consider. Why are you reading size+1 characters from the file when there are only size characters available? Also, tempRead[ size + 1 ] is illegal because array indices start at 0. If you want to null terminate your array, you should still declare the array with size+1 characters, then read size characters and set tempRead[ size ] to '\0'.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    A couple other changes you might consider. Why are you reading size+1 characters from the file when there are only size characters available?
    Probably because you have to do that for strings. Another example of people blindly applying information without understanding it.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    In C++ you would normally use a vector<char> (an array allocated with new[] is another option).
    Thanks for the advice and explanations, Daved. I'll use vectors....

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The syntax for using a vector<char> with read() is a little funky since read expects a plain C style array:
    Code:
    std::vector<char> tempRead(size + 1);
    fin.read(&tempRead[0], size);
    tempRead[size] = '\0';

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    It also seems a little funky as to how you output the vector contents thru ofstream. The >> operator doesn't work with vectors?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    read this page about vector class to see what operators and functions you can use: clicky.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Read. Along with a few other pages. I suppose that I'll use them when the situation warrants or benefits from their useage (vectors), but with this particular program they're not incredibly useful.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends on what you are trying to do. If you want the characters to be output like a string, then output the same way I showed the read (fout << &vec[0]). However, in that case you might want to just assign the contents to a string for easier usage.

    The benefit of having vectors in this program is that they make the dynamic sizing of the array easier. You can use the non-standard variable length array you used in the beginning, you can use vectors, or you can use new [] and delete []. The first option is non-standard (and won't work on VC++ for example). The last option is more error prone than a vector<char> (especially when it comes to making sure the memory is properly freed). That's why I'd probably use vector. I would assign it to a string if I wanted to continue to use the data as a string.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Well, it seems you've put effort into making point, so I can't readily dismiss it. I'm not doing any data manipulation per say here. At this point in the program, my only goal is too buffer the file contents, close the file, reopen it and write out a string (truncated) and then append the original file contents back.

    To put it plainly, it allows me to create a file with reverse data_blocks in relation to another whereas all data_blocks are appended in sequence. So, in the end vectors can do this...

    I do appreciate your input, Daved.

    *... and tempRead.clear(); // would be correct then?
    Last edited by Oldman47; 04-22-2007 at 09:48 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> would be correct then?
    To clear the vector, yes. But be careful. That makes the size of the vector 0, which means you can't read into it (it is like a 0 size array). I still don't understand why you need to clear the vector (or array). Unless I understand you wrong, that won't be necessary here.

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    This code is inside a function which is called upon more than once for each session. So, I'd need to be able to clear the contents, buffer more data, output, rinse & repeat. Wouldn't the vector be resized automatically with the next function call?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It will also be cleared automatically with each function call. Whether it is an array or a vector, it is a local object. Each time the function is called a completely distinct instance is created that is unrelated to the instance created during the last function call.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    ...It will also be cleared automatically with each function call...
    Ah, yes. Since it's local then there's no need.

    Thanks, for all the advice.

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Oldman47 View Post
    Well, it seems you've put effort into making point, so I can't readily dismiss it. I'm not doing any data manipulation per say here. At this point in the program, my only goal is too buffer the file contents, close the file, reopen it and write out a string (truncated) and then append the original file contents back.

    To put it plainly, it allows me to create a file with reverse data_blocks in relation to another whereas all data_blocks are appended in sequence. So, in the end vectors can do this...
    I'm probably coming in a little late to the party, but I wonder if you couldn't get more mileage from a stringstream?

    Code:
    ifstream fin(DRevrNote.c_str());
    stringstream b;
    if (fin.good())
        b << fin.rdbuf();          // slurp the whole file
    
    fin.close();
    
    ofstream fout(DRevrNote.c_str());
    if (fout.good())
        fout << "prepend\n" << b.rdbuf();
    Just a thought (not tested)...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Simple (?) problem rendering a vertex buffer
    By Dark_Phoenix in forum Game Programming
    Replies: 4
    Last Post: 08-11-2007, 07:32 PM
  3. clearing buffer after reading string w/ scanf()
    By fisheromen1031 in forum C Programming
    Replies: 11
    Last Post: 08-01-2005, 09:33 AM
  4. Clearing the Input buffer
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 12:08 PM
  5. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM