解释我的目的:
我的程序有两个部分首先从用户读取两个单词(第二个单词是第一个单词的意思)并将这个单词保存在两个文本文件中第二部分显示或删除单词作为用户的愿望。
我的问题是什么?:
我的程序完美无缺,但几天之后会发生这样的事情:
当我尝试从.txt文件中删除两个单词时(我的意思是1个单词的含义)第一个单词将删除但第二个单词(含义)不会删除,它听起来像.txt文件损坏只会修复每个东西的东西是删除该文本文件并创建新文件,这非常令人讨厌,这意味着丢失所有信息
注意:第一个单词和第二个单词在单独的文件中。第二个词仍然能够表现出来。
我尝试了什么?:
我尝试检查是第二个单词文本文件打开或关闭。听起来它会在正确的时间打开或关闭
检查文本文件是否为只读。那不是。
我不知道这里到底发生了什么:( !!!!
这是我的删除代码我错过了什么?
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <QFile>
#include <QTextStream>
#include <QIODevice>
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
void newFunc();
void restartApp();
void makeFa();
int xy=-1;
ifstream sfile("en.txt");
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
newFunc();
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void newFunc(){
QFile check("EnTemp.txt");
if(check.exists()){
}
else{
ofstream EnTemp("EnTemp.txt");
}
}
void restartApp()
{
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
}
void MainWindow::on_Enter_clicked()
{
if((ui->InEN->text().isEmpty()) || (ui->InFa->text().isEmpty())){
QMessageBox msg;
msg.setText("Fields are empty");
msg.exec();
}
else{
QString en,fa;
en=ui->InEN->text();
fa=ui->InFa->text();
// saving EN words
QFile dataE("en.txt");
dataE.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream streamE(&dataE);
streamE<<en<<"\r\n";
dataE.close();
// saving FA words
QFile DataF("fa.txt");
DataF.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream streamF(&DataF);
streamF.setCodec("UTF-8");
streamF<<fa<<"\r\n";
DataF.close();
ui->InEN->clear();
ui->InFa->clear();
}
}
void MainWindow::on_Next_clicked()
{
QFile EnTemp("EnTemp.txt");
EnTemp.open(QIODevice::WriteOnly);
QTextStream GetTemp(&EnTemp);
if(!sfile.eof()){
string get;
getline(sfile,get);
ui->ShowEn->setText(QString::fromStdString(get));
ui->ShowFa->clear();
xy++;
GetTemp<<xy;
EnTemp.close();
}
else{
restartApp();
}
}
void MainWindow::on_mean_clicked()
{
cout<<xy;
QFile openFa("fa.txt");
QString getFa;
openFa.open(QIODevice::ReadWrite);
QTextStream pointfa(&openFa);
pointfa.setCodec("UTF-8");
int forword=0;
while(forword<=xy){
getFa=pointfa.readLine();
forword++;
}
ui->ShowFa->setText(getFa);
openFa.close();
}
void MainWindow::on_delete_2_clicked()
{
makeFa();
ofstream tempE;
if(!ui->ShowEn->text().isEmpty() | !ui->ShowFa->text().isEmpty())
{
QMessageBox msg;
int ret;
msg.setText("Are you sure you want to delete this words?");
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msg.setIcon(QMessageBox::Warning);
ret= msg.exec();
switch(ret){
case QMessageBox::Yes:
QString input;
input=ui->ShowEn->text();
string line;
ifstream DataE;
DataE.open("en.txt");
tempE.open("tempE.txt");
while(getline(DataE,line)){
if(line != input.toStdString()){
tempE<<line<<"\r\n";
}
}
QString inputF;
inputF= ui->ShowFa->text();
string lineF;
ifstream DataF;
ofstream tempF("tempF.txt");
DataF.open("fa.txt");
while(getline(DataF, lineF)){
if( lineF != inputF.toStdString()){
tempF<<lineF<<"\r\n";
}
}
DataE.close();
DataF.close();
tempE.close();
tempF.close();
sfile.close();
remove("en.txt");
remove("fa.txt");
rename("tempE.txt", "en.txt");
rename("tempF.txt", "fa.txt");
break;
}
}
else{
QMessageBox mass;
mass.setText("Fields are empty");
mass.exec();
}
restartApp();
}
void MainWindow::on_Restore_clicked()
{
xy= 0;
int reach=0;
string get;
QString s;
ifstream openTemp("EnTemp.txt");
openTemp>>xy;
while(reach<=xy){
getline(sfile,get);
reach++;
}
ui->ShowEn->setText(QString::fromStdString(get));
}
void makeFa(){
QFile tempF;
tempF.setFileName("tempF.txt");
tempF.open(QIODevice::ReadWrite | QIODevice::Append);
QTextStream stream(&tempF);
stream.setCodec("UTF-8");
tempF.close();
}
我在Windows 8.1上
当我尝试删除第二个.txt文件中的所有信息时,我发现了一些新内容,文件大小将是3个字节而不是0个字节
答案 0 :(得分:0)
否则为空文件的3个字节可能与UTF-8 BOM匹配(不是说它不知道所述字节,但它是一个强烈的提示)。您的删除功能可能没问题(虽然您应该使文件过滤另一个功能以便清楚,DRY),但是只能使用ascii charset,因为您依赖于std :: i / ofstream和std ::字符串。
你可能(再次,没有确定,只是一个疯狂的猜测)你正在用QFile创建一个文件,并且你提出了一个&#34;意思&#34;包含非ascii字符,因此QFile会自动将文件转换为以BOM为前缀的UTF-8。
如果您不确定此线索,是否介意使用文件创建/更新方法更新帖子?
[编辑]另一个选择是你用ofstream写非ascii字符,这可能会导致&#34;含义&#34;没有找到,因此文件被完全复制。