如何在C ++中更新文件中的行? [英] How Do I Update A Line In A File In C++?

查看:98
本文介绍了如何在C ++中更新文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的情况。我有一个名为(try.txt)的文件,我在该文件中的数据基本上是该文件中的名称和性别。现在,我的目标是从该文件中搜索名称,然后询问用户是否要替换找到的字符串中的任何内容。说,我想替换名称并更新新名称。现在,我使用的想法是我创建了一个临时文件(temp.txt),其中将存储更新的记录,然后将删除旧文件并给出新文件(temp.txt)名称(try.txt)。但问题是当我执行搜索并替换部分时,(try.txt)中的剩余记录将被删除,并且永远不会被添加到(temp.txt)。我该如何解决这个问题?请帮帮我!



---------------------------- -------------------------------------------------- -



  #include   <   iostream  >  
#include < fstream >
#include < string >

使用 命名空间 std;

ifstream in ;
字符串行;
字符串搜索;
字符串名称,性别;
ofstream book; // 这是我暂时存储记录的临时文件
void input()
{
ofstream out;
book.open( temp.txt,ios :: app);
// out.open(try.txt);
< span class =code-comment> // cout<<输入名称和性别<< endl;
// cin>> name>>性别;

// out<< name<<\t<<<< gender<< ; endl;
in .open( try.txt);

if in .is_open())
{
while (getline( in ,line))
{
cout< ;<线474;< ENDL;
book<< line<< endl;
}

book.close();
in .close();

}


}


void find ()
{

cout<< 输入您想要的ID搜索<< ENDL;
cin>>搜索;

in .open( try.txt);
book.open( temp.txt);

while (在>>名称>>性别)
{

if (search.compare(name)== 0
{
cout<< 匹配找到<< endl;
cout<< name<< gender<< endl;

cout<< 输入要替换的单词< ;< ENDL;
cin>> name;
book<< name<< \t<< gender< ;< ENDL;
in .close();
book.close();

remove( try.txt);
重命名( temp.txt try.txt);





}




}










}





int main()
{


输入();

find();



system( pause) ;












}

解决方案

你可以在文本文件中改变一行 - 任何文本文件! 行是文件内容的人工外部解释:换行符只是另一个字节(或一对字节,具体取决于系统)

为了做你想做的事,你必须:

1)创建新文件。

2)将旧文件复制到新文件,直到要更改的行开头

3)将新行写入新文件。

4)跳过旧文件中的旧行。

5)复制剩余部分旧文件到新文件。

6)关闭旧文件和新文件。

7)删除旧文件,并重命名新文件。

So, this is my situation. I have a file called ("try.txt") and i have a data in that file basically names and gender in that file. Now, my goal is to search a name from that file and then ask the user if he wants to replace anything in the found string. Say, i want to replace the name and update a new name. Now, the idea that i used is that i created a temporary file("temp.txt") where the updated record will be stored and then the old file will be deleted and the new file ("temp.txt") will be given the name("try.txt"). But the problem is when i do the search and replace part, the remaining records in the ("try.txt") gets deleted and never gets added to the ("temp.txt"). How can i fix this issue? Please help me out!.

-------------------------------------------------------------------------------

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

ifstream in;
string line;
string search;
string name,gender;
ofstream book; //this is my temporary file where the record will be stored temporarily
void input()
{
	ofstream out;
	book.open("temp.txt",ios::app);
	//out.open("try.txt");
	//cout<<"Enter the name and gender"<<endl;
	//cin>>name>>gender;

	//out<<name<<"\t"<<"  "<<gender<<endl;
	in.open("try.txt");

    if(in.is_open())
	{
		while(getline(in,line))
		{
			cout<<line<<endl;
			book<<line<<endl;
		}

		book.close();
	    in.close();

	}


}


void find()
{

	cout<<"Enter the id you want to search"<<endl;
	cin>>search;

	in.open("try.txt");
	book.open("temp.txt");

	while(in>>name>>gender)
	{
		
		if(search.compare(name) == 0)
		{
			cout<<"Match found"<<endl;
			cout<<name<<gender<<endl;

			cout<<"Enter the word you want to replace"<<endl;
			cin>>name;
			book<<name<<"\t"<<gender<<endl;
			in.close();
			book.close();

			remove("try.txt");
			rename("temp.txt","try.txt");





		}




	}

	








}





int main()
{

	
	input();

	find();



	system("pause");












}

解决方案

You can;t just "change a line" in a text file - any text file! "Lines" are an artificial external interpretation of the file content: newline characters are just another byte (or pair of bytes depending on the system)
In order to do what you want, you have to:
1) Create the new file.
2) Copy the old file to the new file up to the start of the "line" you want to change
3) Write the "new" line to the new file.
4) Skip the "old" line in the old file.
5) Copy the remainder of the old file to the new file.
6) Close both old and new files.
7) Delete the old file, and rename the new file.


这篇关于如何在C ++中更新文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆