Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取文件时的额外字符。C++;_C++_Qt_File Io_Ifstream_Qtcore - Fatal编程技术网

C++ 读取文件时的额外字符。C++;

C++ 读取文件时的额外字符。C++;,c++,qt,file-io,ifstream,qtcore,C++,Qt,File Io,Ifstream,Qtcore,我正在编写两个程序,它们通过读取另一个编写的文件进行通信 我的问题是,当另一个程序读取第一个程序创建的文件时,它会在最后一个数据的末尾输出一个奇怪的字符。这似乎只是随机发生的,因为向文本文件添加数据可能会导致正常输出 我使用C++和QT4。这是程序1的一部分: std::ofstream idxfile_new; QString idxtext; std::string fname2="some_textfile.txt"; //Imported from a file browser i

我正在编写两个程序,它们通过读取另一个编写的文件进行通信

我的问题是,当另一个程序读取第一个程序创建的文件时,它会在最后一个数据的末尾输出一个奇怪的字符。这似乎只是随机发生的,因为向文本文件添加数据可能会导致正常输出

<>我使用C++和QT4。这是程序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();
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();
方案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
}
如果我添加更多数据,它有时会起作用。有时它可以输出数据,例如

3592.�


最后。因此,读取整个数据也不一致。起初我认为它没有正确阅读eof,我已经阅读并尝试了许多类似问题的解决方案,但无法使其正常工作。

谢谢大家的帮助

今天早上我设法自己解决了这个问题。 对于有相同问题的人,我将发布我的解决方案。 创建文件时出现的问题是UTF-8编码。以下是我的解决方案:

计划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();
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();
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()//启用Utf8编码

idxfile_new两个程序是否同时访问文件?在打印或写入输出文件时,为什么要使用
c_str
?Streams可以直接处理
std::string
。程序如何同步访问该文件?@VittorioRomeo该程序从不同时访问该文件。@JoachimPileborg My bad。为了以防万一,我试着去解决它。
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();