修改文件的内容 [英] Modifying the contents of a file

查看:54
本文介绍了修改文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改一个文件的内容,用一个字符串替换所有出现的字符串

。我写了这些函数:


bool read_file(std :: string name,std :: string& s);

bool write_file(std :: string name,const std :: string& s);

void find_replace(std :: string& s,std :: string first,std :: string second);


bool find_replace_file(std :: string name,std :: string first,std :: string

second)

{

std :: string s;

if(!read_file(name,s))

返回false;

find_replace(s,first ,第二); //首先用s替换第二个

返回write_file(name,s);

}


我有没有权利理念?如果没有,有什么不对?谢谢。

解决方案

" Jason Heyes" < JA ******** @ optusnet.com.au>在消息中写道

news:42 *********************** @ news.optusnet.com.au:

我想修改一个文件的内容,用一个字符串替换所有出现的字符串。我写了这些函数:

bool read_file(std :: string name,std :: string& s);
bool write_file(std :: string name,const std :: string& ; s);
void find_replace(std :: string& s,std :: string first,std :: string second);

bool find_replace_file(std :: string name,std :: string first,std :: string
second)
{
std :: string s;
if(!read_file(name,s))
返回false ;
find_replace(s,first,second); //首先用第二个替换s
返回write_file(name,s);
}

我有正确的想法吗?如果没有,有什么不对?谢谢。




如果文件中有多行文字你读取了b
'看看它会如何运作。我认为你要b / b
需要吸取原始的内容。将文件放入容器中,

,例如向量或字符串列表;对字符串进行操作;

然后将结果写入新文件或覆盖旧文件




当然,你可以打开一个文件进行读写,一次一行操作

;但我不想留下文件

打开那么久。如果出现问题会怎样?你碰到重置按钮,或者Windows崩溃了吗?你可能会丢失数据。

所以我倾向于将所有数据吸入RAM,关闭文件,

对数据进行操作,打开输出文件,写入,关闭。


我编写了你所描述的程序,我的基本

运作方式就是这样的(混合C ++和

pseudocode):


std :: ifstream IFS(InputFile);

std :: string Buffer;

的std ::列表<的std :: string>文字;

而(IFS)

{

getline(IFS,缓冲区);

if(IFS。 eof())休息;

Text.push_back(缓冲区);

}

IFS.close();

typedef std :: list< std :: string> :: const_iterator LSCI;

for(LSCI i = Text.begin(); i!= Text.end(); ++ i)

{

//一次迭代一行文字,随意替换

//东西。 (* i)将始终引用

//当前文本行。

}


//现在将处理后的文本写入输出文件:

std :: ofstream OFS (OutputFile);

for(LSCI i = Text.begin(); OFS&& i!= Text.end(); ++ i)

{

OFS<< (* i)<<结束;

}

OFS.close();


我认为这样的事情应该对你有用。 />
-

干杯,

Robbie Hatley

美国加利福尼亚州塔斯廷

email :lonewolfintj在pacbell dot net

web:home dot pacbell dot net slant earnur slant


---- ==发布于Newsfeeds.Com - 无限制 - 未经审查的安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东和西海岸服务器农场 - 通过加密的总隐私= ----


实际上,我上一篇文章中有一个明显的错误:

当迭代一个字符串列表,改变它们时,

一个人不能使用const_iterator;常规迭代器
必须使用
。 (const_iterator可以将

写入字符串到文件,因为列表没有被改变。)所以我应该写:


std :: ifstream IFS(InputFile);

std :: string Buffer;

std :: list< std :: string>文字;

而(IFS)

{

getline(IFS,缓冲区);

if(IFS。 eof())休息;

Text.push_back(缓冲区);

}

IFS.close();


//注意NON-const迭代器所以我们可以写入列表:

typedef std :: list< std :: string> :: iterator LSI;

for(LSI i = Text.begin(); i!= Text.end(); ++ i)

{

//迭代文本一行一次,随意更换

//东西。 (* i)将始终引用

//当前文本行。

}


//现在将处理后的文本写入输出文件:

std :: ofstream OFS (OutputFile);

typedef std :: list< std :: string> :: const_iterator LSCI;

for(LSCI i = Text.begin(); OFS& & i!= Text.end(); ++ i)

{

OFS<< (* i)<<结束;

}

OFS.close();


-

干杯,< br $> b $ b Robbie Hatley

美国加利福尼亚州塔斯廷

电子邮件:来自pacbell dot net的lonewolfintj

web:home dot pacbell dot net slant earnur slant


---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- = East和West-Coast服务器场 - 通过加密实现全面隐私= ----


Robbie Hatley写道:

" Jason Heyes" < JA ******** @ optusnet.com.au>在消息中写道
新闻:42 *********************** @ news.optusnet.com.au:

我想修改一个文件的内容,用一个字符串替换所有出现的字符串。我写了这些函数:

bool read_file(std :: string name,std :: string& s);
bool write_file(std :: string name,const std :: string& ; s);
void find_replace(std :: string& s,std :: string first,std :: string second);

bool find_replace_file(std :: string name,std :: string first,std :: string
second)
{
std :: string s;
if(!read_file(name,s))
返回false ;
find_replace(s,first,second); //首先用第二个替换s
返回write_file(name,s);
}

我有正确的想法吗?如果没有,有什么不对?谢谢。



是的,我认为你确实有正确的想法。

如果文件中有多行文字你就是''阅读,我不明白这是怎么回事。我想你会吮吸原始的内容。将文件放入容器中,例如向量或字符串列表;对字符串进行操作;
然后将结果写入新文件或覆盖旧文件。




我不喜欢认为将整个文件读成

单字符串有什么问题。如果您的处理需要将文件视为

序列,那么容器< string>可能是合适的,但在

这种情况​​下,我认为这是一种不必要的复杂情况。


--Phil。


I would like to modify the contents of a file, replacing all occurances of
one string with another. I wrote these functions:

bool read_file(std::string name, std::string &s);
bool write_file(std::string name, const std::string &s);
void find_replace(std::string &s, std::string first, std::string second);

bool find_replace_file(std::string name, std::string first, std::string
second)
{
std::string s;
if (!read_file(name, s))
return false;
find_replace(s, first, second); // replace first with second in s
return write_file(name, s);
}

Have I got the right idea? If not, what is wrong? Thanks.

解决方案

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42***********************@news.optusnet.com.a u :

I would like to modify the contents of a file, replacing all occurances of
one string with another. I wrote these functions:

bool read_file(std::string name, std::string &s);
bool write_file(std::string name, const std::string &s);
void find_replace(std::string &s, std::string first, std::string second);

bool find_replace_file(std::string name, std::string first, std::string
second)
{
std::string s;
if (!read_file(name, s))
return false;
find_replace(s, first, second); // replace first with second in s
return write_file(name, s);
}

Have I got the right idea? If not, what is wrong? Thanks.



If there''s more than one line of text in the file you''re
reading, I don''t see how that would work. I''d think you''d
need to suck the contents of the "raw" file into a container,
such as a vector or list of strings; operate on the strings;
then write the results, either to a new file or over-writing
the old one.

Of course, you can open a file for read-write, and operate
on it one line at a time; but I prefer not to leave files
open that long. What happens if there''s a problem? You bump
the reset button, or Windows crashes? You could loose data.
So I tend to suck all the data into RAM, close the file,
operate on the data, open the output file, write, close.

I''ve written programs that do what you describe, and my basic
modus operandi has been something like this (mixed C++ and
pseudocode):

std::ifstream IFS (InputFile);
std::string Buffer;
std::list<std::string> Text;
while (IFS)
{
getline(IFS, Buffer);
if(IFS.eof()) break;
Text.push_back(Buffer);
}
IFS.close();
typedef std::list<std::string>::const_iterator LSCI;
for (LSCI i = Text.begin(); i != Text.end(); ++i)
{
// iterate through Text one line at a time, replacing
// stuff at will. (*i) will always refer to the
// "current" line of text.
}

// Now write the processed text to an output file:
std::ofstream OFS (OutputFile);
for (LSCI i = Text.begin(); OFS && i != Text.end(); ++i)
{
OFS << (*i) << endl;
}
OFS.close();

Something like that should work for you, I think.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Actually, there''s one glaring error in my last post:
When iterating through a list of strings, altering them,
one can NOT use a const_iterator; a regular iterator
must be used instead. (The const_iterator is fine for
writing strings to file, because the list is not being
altered.) So I should have written:

std::ifstream IFS (InputFile);
std::string Buffer;
std::list<std::string> Text;
while (IFS)
{
getline(IFS, Buffer);
if(IFS.eof()) break;
Text.push_back(Buffer);
}
IFS.close();

// Note NON-const iterator so we can WRITE to list:
typedef std::list<std::string>::iterator LSI;
for (LSI i = Text.begin(); i != Text.end(); ++i)
{
// iterate through Text one line at a time, replacing
// stuff at will. (*i) will always refer to the
// "current" line of text.
}

// Now write the processed text to an output file:
std::ofstream OFS (OutputFile);
typedef std::list<std::string>::const_iterator LSCI;
for (LSCI i = Text.begin(); OFS && i != Text.end(); ++i)
{
OFS << (*i) << endl;
}
OFS.close();

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


Robbie Hatley wrote:

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42***********************@news.optusnet.com.a u :

I would like to modify the contents of a file, replacing all occurances of
one string with another. I wrote these functions:

bool read_file(std::string name, std::string &s);
bool write_file(std::string name, const std::string &s);
void find_replace(std::string &s, std::string first, std::string second);

bool find_replace_file(std::string name, std::string first, std::string
second)
{
std::string s;
if (!read_file(name, s))
return false;
find_replace(s, first, second); // replace first with second in s
return write_file(name, s);
}

Have I got the right idea? If not, what is wrong? Thanks.


Yes, I think you do have the right idea.
If there''s more than one line of text in the file you''re
reading, I don''t see how that would work. I''d think you''d
need to suck the contents of the "raw" file into a container,
such as a vector or list of strings; operate on the strings;
then write the results, either to a new file or over-writing
the old one.



I don''t think there''s anything wrong with reading the entire file into a
single string. If your processing needed to treat the file as a
sequence of lines then a container<string> might be appropriate, but in
this case I think it is an unnecessary complication.

--Phil.


这篇关于修改文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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