用fstream初始化字符串或字符串流 [英] initialize a string or a stringstream with fstream
问题描述
我想根据文件的整个文本内容,在字符串
类中找到一些查找和擦除操作。
I做了一个函数,它将采用一个字符串(旨在保存整个
文件的内容)
现在我的问题是如何获取所有文件内容一下子和
把它放在一个字符串中进行编辑?
我讨厌通过文件并逐行复制:
string s = file.getline()
while(!file.eof)
{
s + = file。 getline();
}
看起来效率低下......
不是有一些方法可以初始化包含
文件内容的字符串?
喜欢:
string s(file.some数据缓冲区)?
如果我将文件中的内容复制到字符串然后从
字符串复制到字符串流,最后从字符串流复制到个人
变量我是必须的盟友将文件复制4次,对于大型文件来说可能会发生大的问题......
< a href =mailto:cp *** @ austin.rr.com> cp *** @ austin.rr.com 写道:我想做一些查找和删除我在字符串
类中找到的操作,在文件的整个文本内容上。
我创建了一个函数,它将采用一个字符串(旨在保存整个
文件)内容)
我现在的问题是如何一举抓取所有文件内容并将其放入字符串进行编辑?
你不能将字符串定义为整个文件的大小和
然后再做
file.read(s.data(),s.size());
(或类似的东西)??
[.. ]
V
-
请在回复e-时删除资金'A'邮件
我没有回复最热门的回复,请不要问
Victor Bazarov写道:cp***@austin.rr.com 写道:我想在文件的整个文本内容中找到我在字符串
类中找到的一些查找和擦除操作。
我创建了一个带字符串的函数(旨在保存整个
文件内容)
我现在的问题是如何一举抓取所有文件内容并将其放入字符串进行编辑? / blockquote>
无法你要将字符串定义为整个文件的大小,然后
file.read(s.data(),s.size());
(或类似的东西)??
[..]
V
-
请在通过电子邮件回复时删除资金A
我没有回复最热门的回复,请不要问
string :: data()似乎是const
cp *** @ austin.rr.com 写道:Victor Bazarov写道:cp *** @ austin.rr.com 写道:我想做一些我在
字符串类中找到的查找和擦除操作,关于文件的整个文本内容。
我做了一个函数,它将带一个字符串(旨在保存整个文件的内容)
我的问题现在我我如何一举抓取所有文件内容并将其放入字符串进行编辑?
你不能将字符串定义为大小整个文件
然后再做
file.read(s.data(),s.size());
(或类似的东西) ??
[..]
V
-
请删除大写A的当通过电子邮件回复
我没有回复最热门的回复,请不要问
string :: data()似乎是const
哦......对......好吧,那你可能会坚持使用''getline''。
你试过吗?给它一个不同的分隔符''\ n''?像0或
其他不存在的东西?它应该读取整个文件一个
猛扑...
V
-
请在通过电子邮件回复时删除资金''A'
我没有回复最热门的回复,请不要问
I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.
I made a function that will take a string (designed to hold the entire
file contents)
my problem now is how can i grab all the file contents in one swoop and
put it in a string for editing?
I hate to go through the file and copy line by line:
string s = file.getline()
while(!file.eof)
{
s += file.getline();
}
That seems aweful inefficient...
isnt there some way to initialize the string with the contents of the
file?
like:
string s(file.some data buffer)?
If I copy the contents from a file to a string and then later from a
string to a stringstream and finally from a stringstream to individual
variables I am essentially copying the file 4 times, that could stink
for large files...
解决方案cp***@austin.rr.com wrote:I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.
I made a function that will take a string (designed to hold the entire
file contents)
my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?
Couldn''t you define the string to be of the size of the whole file and
then do
file.read(s.data(), s.size());
(or something like that) ??
[..]
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
Victor Bazarov wrote:cp***@austin.rr.com wrote:I want to do some find and erase operations that i find in the string
class, upon the entire text contents of a file.
I made a function that will take a string (designed to hold the entire
file contents)
my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?
Couldn''t you define the string to be of the size of the whole file and
then do
file.read(s.data(), s.size());
(or something like that) ??[..]
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
string::data() appears to be const
cp***@austin.rr.com wrote:Victor Bazarov wrote:cp***@austin.rr.com wrote:I want to do some find and erase operations that i find in the
string class, upon the entire text contents of a file.
I made a function that will take a string (designed to hold the
entire file contents)
my problem now is how can i grab all the file contents in one swoop
and put it in a string for editing?
Couldn''t you define the string to be of the size of the whole file
and then do
file.read(s.data(), s.size());
(or something like that) ??[..]
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
string::data() appears to be const
Oh... Right... Well, then you''re probably stuck with using ''getline''.
Have you tried giving it a different separator from ''\n''? Like 0 or
something else non-existent? It should read the whole file in one
swoop...
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
这篇关于用fstream初始化字符串或字符串流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!