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()
{
    fstream fin("1.mp3",ios::in|ios::binary);
    if(!fin.is_open())
 {
  cout << "源文件打开失败" << endl;
  return 0;
 }
    fstream fout("2.mp3",ios::out|ios::binary);
 if(! fin.is_open())
 {
  cout << "目标文件打开失败!" << endl;
  return 0;
 }
    fout<<fin.rdbuf();
    fin.close();
    fout.close();
    return 0;
}

posted @ 2014-01-10 09:09  静之深  阅读(636)  评论(0)    收藏  举报