PDA

View Full Version : possible bug with ifstream



ChasW
31st January 2007, 22:06
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.



#include <QString>
#include <QMessageBox>
#include <QApplication>

#include <string>
#include <fstream>

using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::streamsize;

int main (int argc, char* argv[])
{
QApplication app (argc, argv);
ofstream OF;
OF.open("testdata.txt");
for (int i = 1; i <= 10; ++i)
{
OF << "test line " << i << endl;
}
OF.close();

ifstream IF;
IF.open("testdata.txt");
string line;
streamsize pos;
while (IF.good())
{
getline (IF, line);
IF.peek();

pos = IF.tellg();

QMessageBox::information(0, "line", QString::fromStdString(line));
}
IF.close();

return 0;
}


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

jacek
31st January 2007, 22:38
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?

ChasW
31st January 2007, 23:29
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:

DEFINES += QT_NO_DEBUG_OUTPUT

TEMPLATE = app
TARGET = test
DEPENDPATH += .
INCLUDEPATH += .

SOURCES += main.cpp


Using the same build method, the following also demonstrates the same problem.

//#include <QString>
//#include <QMessageBox>
//#include <QApplication>

#include <string>
#include <fstream>
#include <vector>

using std::endl;
using std::string;
using std::ifstream;
using std::ofstream;
using std::streamsize;
using std::vector;

int main (int argc, char* argv[])
{
//QApplication app (argc, argv);
ofstream OF;
OF.open("testdata.txt");
for (int i = 1; i <= 10; ++i)
{
OF << "test line " << i << endl;
}
OF.close();

vector<string> vec;

ifstream IF;
IF.open("testdata.txt");
string line;
streamsize pos;
while (IF.good())
{
getline (IF, line);
IF.peek();

pos = IF.tellg();

printf("%s\n", line.c_str());

//QMessageBox::information(0, "line", QString::fromStdString(line));

vec.push_back(line);
}
IF.close();

ofstream OF2;
OF2.open("testdata2.txt");
for (size_t i = 0; i < vec.size(); ++i)
{
OF2 << vec[i] << endl;
}
OF2.close();

return 0;
}


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

ChasW
31st January 2007, 23:46
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?

jacek
1st February 2007, 00:01
This looks similar: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2259

ChasW
3rd February 2007, 07:41
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.