C++读写文件

C++文件处理

C++读写文件

http://www.cnblogs.com/ifeiyun/articles/1573134.html

 

 

C++文件读写详解(ofstream,ifstream,fstream)

 http://blog.csdn.net/kingstar158/article/details/6859379

C++中获得文件大小 

#include <fstream>

using namespace std;

int main(int argc, char* argv[])

{

    ifstream in("file.txt");

    in.seekg(0, ios::end);      //设置文件指针到文件流的尾部

    streampos ps = in.tellg();  //读取文件指针的位置

    cout << "File size: " << ps << endl;

    in.close();                 //关闭文件流

    return 0;

}

 

read&&write的应用

http://blog.sina.com.cn/s/blog_504080b701007z8k.html

 

 

 

 

高手戏玩c++

http://blog.csdn.net/chenhu_doc/article/details/1046370

一次把整个文件读入一个 string

我希望你的答案不要是这样:

string input;
while( !ifs.eof() )
{
    string line;
    getline(ifs, line);
    input.append(line).append(1, '/n');
}

当然了,没有错,它能工作,但是下面的办法是不是更加符合 C++ 的精神呢?

string input(
    istreambuf_iterator<char>(instream.rdbuf()),
    istreambuf_iterator<char>()
);

同样,事先分配空间对于性能可能有潜在的好处:

string input;
input.reserve(10000);
input.assign(
    istreambuf_iterator<char>(ifs.rdbuf()),
    istreambuf_iterator<char>()
);

 

 

 

C++流 rdbuf()

http://blog.sina.com.cn/s/blog_0001988f0100njup.html

 

rdbuf函数有两种调用方法

basic_streambuf<Elem, Traits> *rdbuf( ) const;

basic_streambuf<Elem, Traits> *rdbuf( basic_streambuf<E, T> *_Sb);

1)无参数。返回调用者的流缓冲指针。

2)参数为流缓冲指针。它使调用者与参数(流缓冲指针)关联,返回自己当前关联的流缓冲区指针。

 

 

#include<fstream>
#include<iostream>
using namespace std;
int main()
{
    fstreamfin("1.mp3",ios::in|ios::binary);
   if(!fin.is_open())
 {
  cout<< "源文件打开失败"<< endl;
  return 0;
 }
    fstreamfout("2.mp3",ios::out|ios::binary);
 if(! fin.is_open())
 {
  cout<< "目标文件打开失败!"<< endl;
  return 0;
 }
   fout<<fin.rdbuf();
   fin.close();
   fout.close();
    return0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值