c++ 文件打开方式

文件打开方式 包含在头文件 #include <fstream> 中;

其中包含两个类: ifstream 和 ofsteam;

这两个类分别包含两个方法open(); 和 close(); (打开 or 关闭文件);

类中可以包含类的重载; 例如 ifstream in("txt name",  "ope of open file");

 

第二个参数为打开方式, 包含这几个 ;

ios::in-- 打开一个可读取文件 

ios::out-- 打开一个可写人文件

ios::binary-- 以二进制的形式打开一个文件;

ios::app-- 写入的所有数据将被追加到文件末尾

ios::trunck-- 如果要打开的文件并不存在, 那么以此参数调用open函数将无法进行

ios::nocreate-- 如果打开的文件已经存在, 试图用open函数打开时将返回一个错误

iso::noreplece

 

一次构造包含多个方法  用位运算符 '|' ;

//一次构造多种功能 ;
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    fstream fp("Test.txt", ios::in | ios::out );
    
    if(!fp)
    {
        cerr << "文件打开失败 \n";
        return 0; 
    }
    
    fp<< "Iloveyou!";
    
    static char ch[20]; 
    fp.seekg(ios::beg);  //使得文件指针指向文件头, ios::end指向文件尾 ; 
    fp>> ch;
    
    cout<< ch<< endl;
    fp.close();
    return 0;
} 

 

posted on 2016-04-17 22:12  upupdayday  阅读(1529)  评论(0编辑  收藏  举报

导航