Results 1 to 6 of 6

Thread: possible bug with ifstream

  1. #1
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default possible bug with ifstream

    I recently just installed qt-win-opensource-4.2.2 with the mingw compiler to try out a simple Qt app under Windows XP that I wrote under Linux.

    I think I may have stumbled across a bug, but I am not certain. This minimal program demonstrates what I am observing.

    Qt Code:
    1. #include <QString>
    2. #include <QMessageBox>
    3. #include <QApplication>
    4.  
    5. #include <string>
    6. #include <fstream>
    7.  
    8. using std::endl;
    9. using std::string;
    10. using std::ifstream;
    11. using std::ofstream;
    12. using std::streamsize;
    13.  
    14. int main (int argc, char* argv[])
    15. {
    16. QApplication app (argc, argv);
    17. ofstream OF;
    18. OF.open("testdata.txt");
    19. for (int i = 1; i <= 10; ++i)
    20. {
    21. OF << "test line " << i << endl;
    22. }
    23. OF.close();
    24.  
    25. ifstream IF;
    26. IF.open("testdata.txt");
    27. string line;
    28. streamsize pos;
    29. while (IF.good())
    30. {
    31. getline (IF, line);
    32. IF.peek();
    33.  
    34. pos = IF.tellg();
    35.  
    36. QMessageBox::information(0, "line", QString::fromStdString(line));
    37. }
    38. IF.close();
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    Under Linux the code works as expected. i.e it saves 10 lines to a file then reads the file one line at a time printing the contents of each line in a message box.

    ..but under Windows, using the mingw compiler and qt library, the contents of the string are altered such that only offset portions of the string are being read and printed to the message box.

    This does not occur using pure standard c++.

    I have narrowed down the cause to the use of IF.tellg() which simply gets the current stream position and should have no effect on the stream or file pointer.

    If the line pos = IF.tellg(); (line 34) is removed, the problem goes away.

    Could anyone verify this as being a bug for me or perhaps shed some light on what might be happening?

    Thank you,
    Chas

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: possible bug with ifstream

    Quote Originally Posted by ChasW View Post
    This does not occur using pure standard c++.
    Does this mean that, if you comment out lines with QApplication and QMessageBox, it works OK? Or do you also change the way you build your project?

  3. #3
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: possible bug with ifstream

    Quote Originally Posted by jacek View Post
    Does this mean that, if you comment out lines with QApplication and QMessageBox, it works OK? Or do you also change the way you build your project?
    The comment meant to say that the c++ portions of the code are valid.

    Commenting out those lines does not correct the problem.

    Here is the build file:
    Qt Code:
    1. DEFINES += QT_NO_DEBUG_OUTPUT
    2.  
    3. TEMPLATE = app
    4. TARGET = test
    5. DEPENDPATH += .
    6. INCLUDEPATH += .
    7.  
    8. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    Using the same build method, the following also demonstrates the same problem.
    Qt Code:
    1. //#include <QString>
    2. //#include <QMessageBox>
    3. //#include <QApplication>
    4.  
    5. #include <string>
    6. #include <fstream>
    7. #include <vector>
    8.  
    9. using std::endl;
    10. using std::string;
    11. using std::ifstream;
    12. using std::ofstream;
    13. using std::streamsize;
    14. using std::vector;
    15.  
    16. int main (int argc, char* argv[])
    17. {
    18. //QApplication app (argc, argv);
    19. ofstream OF;
    20. OF.open("testdata.txt");
    21. for (int i = 1; i <= 10; ++i)
    22. {
    23. OF << "test line " << i << endl;
    24. }
    25. OF.close();
    26.  
    27. vector<string> vec;
    28.  
    29. ifstream IF;
    30. IF.open("testdata.txt");
    31. string line;
    32. streamsize pos;
    33. while (IF.good())
    34. {
    35. getline (IF, line);
    36. IF.peek();
    37.  
    38. pos = IF.tellg();
    39.  
    40. printf("%s\n", line.c_str());
    41.  
    42. //QMessageBox::information(0, "line", QString::fromStdString(line));
    43.  
    44. vec.push_back(line);
    45. }
    46. IF.close();
    47.  
    48. ofstream OF2;
    49. OF2.open("testdata2.txt");
    50. for (size_t i = 0; i < vec.size(); ++i)
    51. {
    52. OF2 << vec[i] << endl;
    53. }
    54. OF2.close();
    55.  
    56. return 0;
    57. }
    To copy to clipboard, switch view to plain text mode 

    contents of testdata.txt:
    test line 1
    test line 2
    test line 3
    test line 4
    test line 5
    test line 6
    test line 7
    test line 8
    test line 9
    test line 10

    contents of testdata2.txt
    test line 1
    2
    e 3
    ne 4
    ine 5
    line 6
    line 7
    t line 8
    st line 9
    est line 10

  4. #4
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: possible bug with ifstream

    The more I play around with this, the more it appears to be a mingw bug.

    a simple build using:

    g++ main.cpp -o test

    produces a program that yields the same output as previously listed.

    So I guess this is as good incentive as any to try building the qt library for use with vc++7.1. I realize the open source edition does not officially support msvc, but are there some references to how to do this in some "unofficial" capacity?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: possible bug with ifstream


  6. #6
    Join Date
    Jan 2007
    Posts
    26
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: possible bug with ifstream

    After some more digging I found out that the issue resides with the carriage return character in text mode.

    constructing the ifstream object with ios::in and ios::binary flags seems to have resolved the issue.

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.