Results 1 to 5 of 5

Thread: fstream open/close

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default fstream open/close

    hi, my app is similar this below:
    Qt Code:
    1. fstream file;
    2. while ((choice=getchar()) != '0' ) {
    3. if (!file.open(file_name.c_str(), std::ios_base::in)) { //read only
    4. if ((file()).eof())
    5. cout << "end file \n";
    6. cerr << "unable to open that file\n";
    7. return -1;
    8. }
    9. while ( c=file.get(); ) {
    10. cout.put(ch);
    11. }
    12. f.close();
    13. }
    To copy to clipboard, switch view to plain text mode 
    I read all file and at the end of first cycle I close the file; but the second cycle, program terminate because file seems to be at the end. Why? I supposed if I close and re-open it goes at begin....thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: fstream open/close

    maybe the while loop is faster than the disk operation.
    Try to set a sleep() after f.close().

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: fstream open/close

    how can it be faster?
    Qt Code:
    1. while ((choice=getchar()) != '0' ) {
    To copy to clipboard, switch view to plain text mode 
    here, Program wait for an input......
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: fstream open/close

    try f.flush() before the close then...

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: fstream open/close

    Quote Originally Posted by high_flyer View Post
    try f.flush() before the close then...
    doesn't work but this is ok:
    Qt Code:
    1. file.clear();
    2. file.close()
    To copy to clipboard, switch view to plain text mode 
    Regards

Similar Threads

  1. fstream -> stdout
    By zlatko in forum General Programming
    Replies: 11
    Last Post: 25th January 2006, 05:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.