复制文本文件的更好方法是什么?
我正在将文本从一个文件复制到另一个文件,并进行某些更改。
我有input.txt、tmp.txt 和output.txt。
这个想法是将几行复制到 tmp.txt(直到到达分隔符),将文本从 tmp.txt 移动到 output.txt,擦除 tmp.txt,然后继续循环,直到到达输入末尾。 TXT。
我在使用 tmp.txt 文件时遇到问题。 在我第一次复制内容后,即使我关闭、删除并重新打开文件进行写入,它也会停止接受新文本。 我的代码变得非常混乱。
谁能建议一种巧妙的方法来做到这一点? (处理 tmp.txt,从 tmp.txt 复制,擦除 tmp.txt 继续循环)
Nb 这是一个我被作业问题困扰的子任务 - 我正在从文本文件中删除 C++ 注释。
编辑:对于任何想知道为什么我需要 tmp.txt 的人:如果程序遇到 */
(关闭评论)而没有开放评论,那么它将需要处理之前的所有内容,直到上一个评论,如评论。 我使用 temp 来保存可能是评论也可能不是评论的文本。 如果是,我将删除 tmp 中的文本,如果不是,我将把 tmp 中的文本复制到 output.txt。
I'm copying over text from one file to another, with certain changes.
I have input.txt, tmp.txt and output.txt.
The idea is to copy a few lines over to tmp.txt (until we reach a delimiter), move the text from tmp.txt to output.txt, wipe tmp.txt and then continue the cycle until we reach the end of input.txt.
I'm having trouble with the tmp.txt file. After the first time I copy the contents over it stops accepting new text, even after I've closed, deleted and re-opened the file for writing. My code has become really messy.
Could anyone suggest a neat way of doing this? (coping to tmp.txt, copying from tmp.txt,wiping tmp.txt continuing the cycle)
N.b. This is a subtask that I'm stuck on with a homework problem - I'm removing c++ comments from a text file.
Edit: For anyone wondering why I need the tmp.txt: If the program encounters */
(close comment) without an open comment then it will need to treat everything before it, until the previous comment, as a a comment. I'm using the temp to hold text that may or may not be a comment. If it is- I'll delete the text in tmp, if not, I'll copy the text in tmp to output.txt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 input.txt 作为 istream 打开,将 output.txt 作为 ostream 打开。 开始从输入中读取,回显所有不属于注释的字符。 当您遇到注释时,不要写入输出。 应该可以做到这一点。
Open input.txt as an istream, and output.txt as a ostream. Start reading from input echoing all the characters not belonging to a comment. When you encounter a comment, don't write to the output. That should do it.
如果您使用 C++ fstream 的东西,当您重新打开流时,您可能必须在流上调用 .clear() 以清除错误标志(例如当您点击 EOF 时设置的标志)。
If you are using the C++ fstream stuff, you may have to call .clear() on the stream when you re-open it in order to clear error flags (such as those set when you hit EOF).
我建议您跳过 tmp.txt 文件。 只需打开 input.txt 并将数据随机播放到您认为合适的 output.txt 中就足够了。
I suggest you skip the tmp.txt file. Should be enough to just open input.txt and just shuffle data into output.txt as you see fit.
除非实际删除评论是您作业的一部分,否则我建议您编写一些简单的脚本。 这里是对
sed
脚本的引用。如果作业涉及删除注释,您可以只将输入文件作为流运行,并在注释中停止写入输出文件。
start
和end
开始,//
、C++ 风格实现此功能#if 0
和#endif
块——只是为了好玩:- )Unless it is part of your homework to actually do the comment deletion, i would suggest some simple scripting. Here is a reference to a
sed
script.If the homework involves deleting the comments, you could just run the input file as a stream and stop writing the output file while in a comment.
start
andend
of a comment,//
, C++ style#if 0
and#endif
blocks -- just for kicks :-)