使用fstream覆盖文件中的一些文本并删除文件的其余部分 [英] overwriting some text in a file using fstream and delete the rest of the file

查看:620
本文介绍了使用fstream覆盖文件中的一些文本并删除文件的其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,使用fstream
读取文件,然后重写一些文本并删除文件的其余部分
这是我正在尝试执行的代码

 #include< iostream> 
#include< fstream>

使用namespace std;
int main(int argc,char ** argv){
fstream * binf;
fstream someFile(t.txt,ios :: binary | ios :: out | ios :: in);
int i;
for(i = 0; i <3; i ++){
char c;
someFile.seekg(i);
someFile.get(c);
cout<<<c:<< c<< endl;
}
someFile.seekp(i ++);
someFile.put(Y);
someFile.seekp(i ++);
someFile.put(Y);
//删除文件的其余部分
return 0;

$ / code>

请注意以下用于打开文件的标志

  ios :: in打开输入操作。 
ios :: out打开输出操作。
ios :: binary以二进制模式打开。
ios :: ate设置文件末尾的初始位置。如果该标志没有被设置为任何值,则初始位置是文件的开始位置。
ios :: app所有输出操作都在文件末尾执行,将内容附加到文件的当前内容。此标志只能用于打开输出操作的流。
ios :: trunc如果为输出操作打开的文件以前已经存在,则其之前的内容将被删除,并由新的内容替换。

我尝试了许多这些的组合,但是他们中没有人帮助我做我想要的
我想阅读文件,直到我找到文本。如果我找到了我想要的文本,我就把它写下来,并删除文件的其余部分。所以,这个文件应该被重新设置为更小的文件。

解决方案

/ b>

可能的解决方案:

关闭文件并调用truncate函数:

  #include< unistd.h> 
int ftruncate(int fildes,off_t length);
int truncate(const char * path,off_t length);

MS Windows版本的truncate是 _chsize - 请参阅 http://msdn.microsoft.com/en-us//library/ dk925tyb.aspx

  int _chsize(
int fd,
long size
);

或者打开你的文件只读,读/替换为一些stringstream,然后把所有的东西放到你的文件这个时候打开覆盖:
$ b $ pre $ f $ someFile(t.txt,ios :: binary | ios :: in);
stringstream ss;
//复制(替换)从someFile所需的任何东西到
someFile.close();
someFile.open(t.txt,ios :: binary | ios :: out | ios :: trunc);
someFile<< ss.rdbuf();
someFile.close();


I am trying to write a program that read a file using fstream then, rewrite some of the text and delete the rest of the file This the code that I am trying to do

#include<iostream>
#include<fstream>

using namespace std;
int main(int argc, char **argv){
    fstream *binf;
    fstream someFile("t.txt", ios::binary|ios::out|ios::in);
    int i;
    for(i=0;i<3;i++){
        char c;
        someFile.seekg(i);
        someFile.get(c);
        cout<<"c:"<<c<<endl;
    }
    someFile.seekp(i++);
    someFile.put("Y");
    someFile.seekp(i++);
    someFile.put("Y");
    //Delete the rest of the file
    return 0;
}

Note the following flags for opening a file

ios::in Open for input operations.
ios::out    Open for output operations.
ios::binary Open in binary mode.
ios::ate    Set the initial position at the end of the file. If this flag is not set to any value, the initial position is the beginning of the file.
ios::app    All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.
ios::trunc  If the file opened for output operations already existed before, its previous content is deleted and replaced by the new one.

I try many combinations of these but non of them help me to do what I want I want to read the file until I find text. If I find the text that I want, I over write it and delete the rest of the file. So, the file should be re-sized to smaller file.

解决方案

You can't do that with single stream object.

Possible solutions:

Either close your file and call truncate function, :

 #include <unistd.h>
 int ftruncate(int fildes, off_t length);
 int truncate(const char *path, off_t length); 

MS Windows version of truncate is _chsize - see http://msdn.microsoft.com/en-us//library/dk925tyb.aspx

int _chsize( 
   int fd,
   long size 
);

Or open your file for reading only, read/replace to some stringstream, then put everything to your file this time opened for overwriting:

fstream someFile("t.txt", ios::binary|ios::in);
stringstream ss;
// copy (with replacing) whatever needed from someFile to ss 
someFile.close();
someFile.open("t.txt", ios::binary|ios::out|ios::trunc);
someFile << ss.rdbuf();
someFile.close();

这篇关于使用fstream覆盖文件中的一些文本并删除文件的其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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