在C ++中的.txt中插入一行 [英] Inserting a line in a .txt in C++

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

问题描述

我想在第一行更新.txt文件w / in insetion of text。



我的方法是

1如果他/她想要插入东西,请让用户点a。

2.要求用户输入要插入的文字line_insert

3 fout new_text.txt中的文本

4.将source.txt中的数据复制到new_text.txt

5.将new_text.txt中的数据复制到source.txt所以在下次运行程序时,旧数据不会被覆盖



我的问题是

1.我只能插入文本一次,然后程序自动关闭,尽管我键入系统(暂停)

2.数据根本没有复制...



附上代码。

非常感谢。

I want to update a .txt file w/ an insetion of text in the 1st line.

My method is
1. ask the user to cin "a" if s/he wants to insert things
2. ask the user to input the text to be inserted as "line_insert"
3. fout the text in new_text.txt
4. copy the data in source.txt to new_text.txt
5. copy the data in new_text.txt to source.txt so that upon next running of programme, the old data won''t be overwrite

My problem is that
1. I can only insert text for once then the programme closes automatically despite that I typed system("pause")
2. The data are not copying at all...

attached is the code.
Thanks lots.

#include <time.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{  char key;
int size;
char* buffer;
char line_insert;

    ofstream fout;
    ifstream fin;

fin.open ("source.txt");
fout.open ("new_text.txt");
    while(1){

           
             cin>>key;
             if (key=='a')
             {cin>>line_insert;
  
               fout << line_insert;
               
                 
                 key=='0';
                 
                 
                             fin.seekg(0,ifstream::end); // get size of file

                             size=fin.tellg();
                             fin.seekg(0);

                             buffer = new char [size];// allocate memory for file content


                             fin.read (buffer,size);// read content of infile


                             fout.write (buffer,size);// write to outfile

                            delete[] buffer;// release dynamically-allocated memory

                       
                             fout.close();
                             fin.close();
                             
                             
                             fin.open ("new_text.txt");
                             fout.open ("source.txt");
                 fin.seekg(0,ifstream::end); // get size of file

                             size=fin.tellg();
                             fin.seekg(0);

                             buffer = new char [size];// allocate memory for file content


                             fin.read (buffer,size);// read content of infile


                             fout.write (buffer,size);// write to outfile

                            delete[] buffer;// release dynamically-allocated memory
                       if (key =='b')
                      { break;}
                       }
                       fout.close();
system("PAUSE");
  return 0;


}



已添加代码块[/ Edit]


Code block added[/Edit]

推荐答案

假设你必须使用''streaming'',我会做的有点不同。

读新的第一行并写入文件。

将旧文件附加到这个新的第一个文件,不一定要将整个文件读入内存。

删除旧的源文件。

将新文件重命名为旧文件。





或者,如果你想得到真正的幻想,你可以保留源文件但是''延伸''它的长度是新文本行的大小。现在将现有文件FORWARD中的字节复制到现有文件中,在前面留出空间,现在您可以在其中编写新行。但也许这与整个流媒体概念背道而驰。就像那可能无法通过管道或TCP连接或其他东西。但是,如果我只是处理一个永远存在于硬盘驱动器上的文件,我会放弃流式传输并将文件作为二进制文件来处理,并执行我在此处提到的内容。
Assuming you must use ''streaming'', I would have done this a bit differently.
Read the new first line and write to a file.
Append the old file to this new first file, not necessarily trying to read entire file into memory, either.
Delete the old source file.
Rename the new file as the old file.


Or, if you wanted to get really fancy, you could keep the source file but ''extend'' its length the size of the new line of text. Now copy bytes from existing file FORWARD into existing file, leaving room at front, where you now write the new line. But maybe that goes against this whole ''streaming'' concept. Like that might not work through a pipe or TCP connection or something else. But seriosuly, if I were just working with a file always going to be on a hard drive, I would blow off the ''streaming'' and deal with the file as a binary and do what I mentioned here.


首先,为了一次读取一个字符,从cin流式传输是没有用的,因为每次流式传输都需要按下返回来完成输入。使用 istream :: getline()会容易得多!



其次,如果您希望将文本附加到现有文件,则必须打开它才能附加;请参阅 fstream :: open()。你省略了模式,所以你每次都要替换(覆盖)文件。



三,IO很慢:而不是写你想插入的东西要在文本的每一位之后进行归档,最好在打开输出文件之前将整个文本组装到内存中。你正在做的就像一个编辑器,它会在你键入的每个单词后保存整个文件。由于多种原因,这不是很明智,最重要的是你不必要地一遍又一遍地复制已经存在的文本。
First, for reading one character at a time, streaming from cin isn''t useful since streaming requires a press of return every time to complete the input. It would be much easier to use istream::getline()!

Second, if you wish to append text to an existing file, you have to open it for appending; see fstream::open(). You omitted the mode, so you''re replacing (overwriting) the file every time.

Third, IO is slow: instead of writing the stuff you wish to insert to file after every bit of text, it would be better to assemble the entire text in memory before even opening the output file. What you''re doing is like an editor that saves the entire file after every single word you type. That is not very sensible for multiple reasons, most importantly you needlessly copy the text that''s already in there over and over again.


这篇关于在C ++中的.txt中插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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