Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.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
Windows 新行未写入文件(Qt)_Windows_Visual Studio 2010_Qt - Fatal编程技术网

Windows 新行未写入文件(Qt)

Windows 新行未写入文件(Qt),windows,visual-studio-2010,qt,Windows,Visual Studio 2010,Qt,我使用qtextedit在我的软件中保存活动日志。稍后,当我想使用toPlainText()将其保存为文本时,生成的文本文件是一行,没有任何换行符。 我开始使用明文()记录日志,并使用append()添加后续添加内容 void rocketscience::saveLog(){ QFile日志文件; QTextStream注销; QString日志名; QSettings Preveset(“美国”、“法国”); if(defaultDir.exists(prevSet.value(“setti

我使用qtextedit在我的软件中保存活动日志。稍后,当我想使用toPlainText()将其保存为文本时,生成的文本文件是一行,没有任何换行符。 我开始使用明文()记录日志,并使用append()添加后续添加内容

void rocketscience::saveLog(){
QFile日志文件;
QTextStream注销;
QString日志名;
QSettings Preveset(“美国”、“法国”);
if(defaultDir.exists(prevSet.value(“settings/logPath”).toString())
logfName=QFileDialog::getSaveFileName(这个“保存文件”,fName,“文本(*.txt”);
if(logfName!=NULL){
setFileName(logfName);
打开(QIODevice::WriteOnly);
logOut.setDevice(&logFile);

注销文件可能是使用UNIX行结尾编写的?您应该以文本形式打开文件,以获取本地(Windows)行结尾:

  logFile.open(QIODevice::WriteOnly|QIODevice::Text);

从QTextStream类引用(该行有点隐藏):

注意:在Windows上,如果使用QIODevice::Text标志打开QTextStream的设备或字符串,则所有“\n”字符都将写入“\r\n”

其中“\n”是UNIX行结尾,“\r\n”是Windows行结尾(CR/LF)

删除方法开头的QTextStream初始化,并将if语句更改为:

if (!logfName.isEmpty())
{
    logFile.setFileName(logfName);
    logFile.open(QIODevice::WriteOnly);
    QTextStream logOut(&logFile, QIODevice::Text);
    logOut<<ui.statusReport->toPlainText();
    logFile.close(); 
} 
if(!logfName.isEmpty())
{
setFileName(logfName);
打开(QIODevice::WriteOnly);
QTextStream注销(&logFile,QIODevice::Text);

注销我也有同样的问题。但是,@Tim Meyer给出的解决方案对我不起作用。在该行中:

QTextStream logOut(&logFile, QIODevice::Text);
显示了一个错误。因此,我执行了以下操作并为QT5.8工作:

/**
* Method to save a text file.
* @param asFileName: Complete file path, including name and extension.
* @param asText: Text to be written in the file
* @return true if the save was successful, false otherwise.
*/
bool MainWindow::saveFile(QString asFileName, QString asText)
{
    QFile file(asFileName);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QMessageBox::critical(this,"Error","File could not be opened");
        return false;
    }

    QTextStream out(&file);
    out << asText;
    file.close();

    return true;
}
/**
*方法来保存文本文件。
*@param asFileName:完整的文件路径,包括名称和扩展名。
*@param asText:要写入文件的文本
*@保存成功返回true,否则返回false。
*/
bool主窗口::保存文件(QString asFileName,QString asText)
{
QFile文件(asFileName);
如果(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
QMessageBox::critical(此“错误”,“文件无法打开”);
返回false;
}
QTextStream out(文件(&F);

out因为它是一个局部变量,所以不会为NULL。NULL是指针类型的有效值。可能会有QString重载,但我懒得检查它,所以我说我不确定;)没有采用
QFile
指针和IO设备打开模式的
QTextStream
构造函数。有一个构造函数采用
文件*
,不同于
QFile
。相反,应该将打开模式提供给文件打开-
logFile.open(QIODevice::WriteOnly | QIODevice::Text)
简短、简洁的答案,效果很好。得票最多。
/**
* Method to save a text file.
* @param asFileName: Complete file path, including name and extension.
* @param asText: Text to be written in the file
* @return true if the save was successful, false otherwise.
*/
bool MainWindow::saveFile(QString asFileName, QString asText)
{
    QFile file(asFileName);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QMessageBox::critical(this,"Error","File could not be opened");
        return false;
    }

    QTextStream out(&file);
    out << asText;
    file.close();

    return true;
}