CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Iterators- Help..

    Hi,

    I have a problem in hand now.Any help is appreciated.
    I have 2 files.I have to read data from 1 file & write to another file.Iam doing that right now as follows:

    Opening the "file to read" in a stream.Reading it to a buffer & then writing the buffer to the "output stream".(In Binary mode)
    The code is like as follows:

    // opening the file to read
    ifstream inputFile("File to read",ios::in|ios::binary);
    if(inputFile.is_open())
    {
    istream:os_type beginpos = inputFile.tellg();
    inputFile.seekg(0,ios::end);
    istream:os_type endpos = inputFile.tellg();
    inputFile.seekg(0, ios::beg);
    int numBytes = endpos - beginpos;
    char *buffer= new char[numBytes+1];
    inputFile.read(buffer,numBytes);

    //File to write
    if(!OutPutFile.eof())
    m_ResourceGroup.seekp(0,ios::end);
    OutPutFile.write(buffer,numBytes);
    }

    Now this way of writing is causing performance problems.I would like to use the istream & ostream iterators instead of the "read" & "write" calls.Iam no good in iterators & I havent used it in file operations.

    Can any one show me how to do it with respect to my code above...

    Thanks...

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Hum, what sort of performance problem do you get ?
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Oct 2001
    Posts
    745
    When I check my whole application with a "Performance Analyser",This function takes a lot of percentage time compared to the other function calls.So I want to make this call faster.Lots of file will be read using this Function,Thats why...

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Hum, well, the basic problem is that you are reading and writing to the harddisk, which is inherently slow. I don't think iterators would help speed things up. You might want to eliminate a few of the seek operations and try using c-style fread and fwrite.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Kohinoor, you only seek 3 times. If you are copying large files, then the hard disk is most likely your bottleneck -- as Yves pointed out. If you are copying many, many small files in this manner, then the seeking (which also happens to be related to the hard disk) is probably your bottleneck. I'd be very surprised if your performance had anything to do with C/C++.

    - Kevin

  6. #6
    Join Date
    Oct 2001
    Posts
    745
    mmm..,Okay.I will remove some of the seek operations as u said.This "iteartor" is an idea from my group leader.So he wants that to be done & tested for performance.He is a kind of adamant person.So I have to do it.
    Could u please tell me with respect to my code how,Iterators can be used..

  7. #7
    Join Date
    Sep 2002
    Posts
    1,747
    Do iterators here mean file pointers? Or something more quixotic?

    One way to get better performance, if the files are realatively small compared to memory, would be to read the input file into memory and build the output file in memory. Then, iterators for an stl vector or mere pointer array accessing can be used, and the output can be saved when done. As well, if this is on a Windows system, file mapping may be better still since it will conserve physical memory usage (though your virtual space will still be the same) and map in only those pages you access. Similar functionality can be found on other OS's.

    I hope I have helped!

    [Edit: This only works if this is to be done over and over, which is what I assumed from the performance issue... Sorry I forgot to state on first posting]
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  8. #8
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    I don't believe std::fstream, std::ifstream, or std:fstream have iterators. Anyway, iterators are nice for traversing complex structures such as linked-lists, trees, etc.... But where a simple while loop or a for loop with simple math will do, iterators have no performance gain (and may possibly have a performance hit).

    Personally, I don't see how using iterators (you'd have to create your own here -- yuck) would help.

    - Kevin

    I hope your boss can understand your dilemma.

  9. #9
    Join Date
    Oct 2001
    Posts
    745
    Thanks for ur suggestion.
    By iterators,I meant istream_iterator & ostream_iteartor.

  10. #10
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    That's supposed to read std:: ofstream (without the space)above. CodeGuru automatically replaces the combination of ':' and 'o' with a yawn...

    How does one get around that without inserting a space?

    Also, how does have a graphic displayed by their names in the postings?

  11. #11
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Well, you learn something new every day. The book I learned the STL from only discusses istream_iterator and ostream_iterator for one paragraph -- and that paragraph shows up 16 chapters before istream and ostream. Some book!

    Thanks for enlightening me.

    - Kevin

    P.S. I still don't see how the iterators are going to improve performance...

  12. #12
    Join Date
    Oct 2001
    Posts
    745
    I just read now that istreambuf_ iterator & ostreambuf_iterator does the job.Iam working on binary data.I dont know wheather that will work or not...

  13. #13
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by KevinHall
    That's supposed to read std:: ofstream (without the space)above. CodeGuru automatically replaces the combination of ':' and 'o' with a yawn...

    How does one get around that without inserting a space?

    Also, how does have a graphic displayed by their names in the postings?
    When you post, there is a check-box at the bottom
    Disable Smilies in This Post
    To choose an avatar, go to user cp, Edit Options, Change Avatar.

    For Kohinoor, I think that using stream iterators will only make your performance issue worse, if it does change anything. It might also not change a thing.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  14. #14
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Just a note: I don't know if you left code
    out of your original post or not, but you
    do a "new []" and no "delete []".

  15. #15
    Join Date
    Sep 2002
    Posts
    1,747

    I see you kevin....

    Wow. Something more quixotic that I never heard of. Great lesson here!
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

Page 1 of 2 12 LastLast

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

Featured