在c++中更新和删除文件中的数据
update and delete data from file in c++
如何在不显示旧数据的情况下从c++中的文件更新数据?我想删除特定的数据并更新。例如,我只想更新名称,另一次只更新gpa,删除旧的gpa?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int k=0;
string line;
string find;
char name[25];
int id=0;
float gpa=0;
ofstream myfile;
myfile.open("data.txt");
while(k!=3){
cout<<"press 1 for adding data"<<endl;
cout<<"press 2 for update "<<endl;
cin>>k;
if(k==1)
{
cout<<"enter ID "<<endl;
cin>>id;
cout<<"enter Name"<<endl;
cin>>name;
cout<<"enter GPA "<<endl;
cin>>gpa;
myfile<<name<<endl;
myfile<<id<<endl;
myfile<<gpa<<endl<<endl<<endl;
}
if(k==2)
{
cout<<"name u want to update "<<endl;
cin>>find;
ifstream file;
file.open("data.txt");
while (!file.eof() && line!=find)
{
getline(file,line);
}
cout<<"enter ID "<<endl;
cin>>id;
cout<<"enter Name"<<endl;
cin>>name;
cout<<"enter GPA "<<endl;
cin>>gpa;
myfile<<name<<endl;
myfile<<id<<endl;
myfile<<gpa<<endl<<endl<<endl;
}
if(k==3){
myfile.close();
}
}
return 0;
}
首先,您必须将所有其他记录复制到一个临时文件中,例如temp.txt
,然后删除旧文件data.txt
,并将temp.txt
重命名为data.txt
。现在将新记录写入文件data.txt
。
代码:
if(k==2)
{
cout<<"name u want to update "<<endl;
cin>>find;
ifstream myfile2; //for reading records
myfile2.open("data.txt");
ofstream temp;
temp.open("temp.txt");
while (getline(myfile2, line))
{
if (line != find)
temp << line << endl;
}
myfile2.close();
temp.close();
remove("data.txt");
rename("temp.txt", "data.txt");
//Now add new Record to file
cout<<"enter ID "<<endl;
cin>>id;
cout<<"enter Name"<<endl;
cin>>name;
cout<<"enter GPA "<<endl;
cin>>gpa;
ofstream myfile;
myfile.open("data.txt", ios::app | ios::out);
myfile<<name<<endl;
myfile<<id<<endl;
myfile<<gpa<<endl<<endl<<endl;
}
逐行读取旧文件并将行复制到新文件中。找到要更新的行后,使用行对其进行更改,并将其复制到新文件中。当您到达EOF时,删除旧文件并用旧文件的名称重命名新文件。
它将是这样的:
int replace_line(const char *fname, const char *oldline, const char* newline)
{
int done = 0;
/* Open templorary file */
FILE *newfp = fopen("file.tmp", "a");
if (newfp != NULL)
{
/* Open exiting file */
FILE *oldfp = fopen(fname, "r");
if(oldfp != NULL)
{
ssize_t read;
size_t len = 0;
char * line = NULL;
/* Line-by-line read from exiting file */
while ((read = getline(&line, &len, oldfp)) != -1)
{
if(strstr(line, oldline) == NULL) fprintf(newfp, "%s", line);
else fprintf(newfp, "%s", newline);
}
/* Clean up memory */
fclose(oldfp);
if (line) free(line);
done = 1;
}
fclose(newfp);
remove(fname);
rename("file.tmp", fname);
return done;
}
return 0;
}
相关文章:
- 如何在我从C++文件中读取后立即删除行
- 删除 XML 文件中的子节点C++
- 如何在输入新内容 c++ 之前删除文件中的所有内容
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 如何自动获取我的项目的路径并删除一些文件
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 文件删除(使用取消链接)与释放所有磁盘空间之间的延迟
- 重命名批处理文件(删除名称中的一些符号)(python/matlab/shell)
- 如何找到字符串并使用C 文件中的文件删除它
- 使用 DEF 文件删除 dll 导出的名称
- 关闭后等待文件删除
- c++中的文件删除
- 如何共享文件删除权限时,我打开一个文件ifstream
- 典型的Win32文件删除(IDropTarget::drop)实现
- 生成文件模式规则:循环生成文件.o < - 删除生成文件依赖项