本文介绍了用fstream初始化字符串或字符串流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想根据文件的整个文本内容,在字符串 类中找到一些查找和擦除操作。 I做了一个函数,它将采用一个字符串(旨在保存整个 文件的内容) 现在我的问题是如何获取所有文件内容一下子和 把它放在一个字符串中进行编辑? 我讨厌通过文件并逐行复制: string s = file.getline() while(!file.eof) { s + = file。 getline(); } 看起来效率低下...... 不是有一些方法可以初始化包含 文件内容的字符串? 喜欢: string s(file.some数据缓冲区)? 如果我将文件中的内容复制到字符串然后从 字符串复制到字符串流,最后从字符串流复制到个人 变量我是必须的盟友将文件复制4次,对于大型文件来说可能会发生大的问题...... 解决方案 V - 请在回复e-时删除资金'A'邮件 我没有回复最热门的回复,请不要问 V - 请在通过电子邮件回复时删除资金A我没有回复最热门的回复,请不要问 string :: data()似乎是const 你不能将字符串定义为大小整个文件然后再做 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 stringclass, upon the entire text contents of a file.I made a function that will take a string (designed to hold the entirefile contents)my problem now is how can i grab all the file contents in one swoop andput 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 thefile?like:string s(file.some data buffer)?If I copy the contents from a file to a string and then later from astring to a stringstream and finally from a stringstream to individualvariables I am essentially copying the file 4 times, that could stinkfor large files... 解决方案V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 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 askstring::data() appears to be const 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 constOh... Right... Well, then you''re probably stuck with using ''getline''.Have you tried giving it a different separator from ''\n''? Like 0 orsomething else non-existent? It should read the whole file in oneswoop...V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t ask 这篇关于用fstream初始化字符串或字符串流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-24 02:52