【发布时间】:2013-04-26 12:51:10
【问题描述】:
我一直试图想出一个代码来删除保存在文本文件中的数据,但无济于事。应该怎么做??在 c++ 中,这是我的代码,我该如何改进它,以便删除保存的数据可能是逐个条目?
#include<iostream>
#include<string>
#include<fstream>
#include<limits>
#include<conio.h>
using namespace std;
int main()
{
ofstream wysla;
wysla.open("wysla.txt", ios::app);
int kaput;
string s1,s2;
cout<<"Please select from the List below"<<endl;
cout<<"1.New entry"<<endl;
cout<<"2.View Previous Entries"<<endl;
cout<<"3.Delete an entry"<<endl;
cin>>kaput;
switch (kaput)
{
case 1:
cout<<"Dear diary,"<<endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin,s1);
wysla<<s1;
wysla.close();
break;
}
return 0;
}
【问题讨论】:
-
数据?你的意思是文本文件中的任意行?你不能到位
-
只有一种方法可以从文件中间删除。将整个文件读入内存,删除内存中不需要的部分。从内存中写出整个文件。
-
@brad 不,这不是一个私人项目
-
@john 不,这不是唯一的方法,但它肯定是最简单的方法。
-
@john 这不是我通常这样做的方式,事实上,这不是一个很好的方式。通常的方法是将文件复制到临时文件中,在复制时即时进行更改。然后关闭临时文件,只有在关闭后输出流是好的,删除原来的,重命名临时文件。
标签: c++