Search code examples
c++qtfile-ioifstreamqtcore

Extra character when reading a file. C++


I'm writing two programs that communicate by reading files which the other one writes.

My problem is that when the other program is reading a file created by the first program it outputs a weird character at the end of the last data. This only happens seemingly at random, as adding data to the textfile can result in a normal output.

I'm utilizing C++ and Qt4. This is the part of program 1:

std::ofstream idxfile_new;
QString idxtext;
std::string fname2="some_textfile.txt";    //Imported from a file browser in the real code.
idxfile_new.open (fname2.c_str(), std::ios::out);
idxtext = ui->indexBrowser->toPlainText(); //Grabs data from a dialog of the GUI.
                                           //See 'some_textfile.txt' below
idxfile_new<<idxtext.toStdString();
idxfile_new.clear();
idxfile_new.close();

some_textfile.txt:

3714.1 3715.1 3716.1 3717.1 3719.1 3739.1 3734.1 3738.1 3562.1 3563.1 3623.1 

part of program 2:

std::string indexfile = "some_textfile.txt";    //Imported from file browser in the real code
std::ifstream file;
std::string sub;
file.open(indexfile.c_str(), std::ios::in);
while(file>>sub)
{
    cerr<<sub<<"\n";    //Stores values in an array in the real code
}

This outputs:

3714.1
3715.1
3716.1
3717.1
3719.1
3739.1
3734.1
3738.1
3562.1
3563.1
3623.1�

If I add more data it works at times. Sometimes it can output data such as

3592.�

or

359�

at the end. So it is not consistent in reading the whole data either. At first I figured it wasn't reading the eof properly, and I have read and tried many solutions to similar problems but can't get it to work correctly.


Solution

  • Thank you guys for the help!

    I managed to solve the problem by myself this morning. For anyone with the same problem I will post my solution. The problem was the UTF-8 encoding when creating the file. Here's my solution:

    Part of program 1:

    std::ofstream idxfile_new;
    QString idxtext;
    std::string fname2="some_textfile.txt";
    idxfile_new.open (fname2.c_str(), std::ios::out);
    idxtext = ui->indexBrowser->toPlainText(); 
    
    QByteArray qstr = idxtext.toUtf8(); //Enables Utf8 encoding
    idxfile_new<<qstr.data();
    idxfile_new.clear();
    idxfile_new.close();
    

    The other program is left unchanged. A hex converter displayed the extra character as 'ef bf bd', which is due to the replacement character U+FFFD that replace invalid bytes when encoding to Utf8.