15145641

  给出了比较常见的文件操作,包括二进制文件操作。代码如下:

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<fstream>
using namespace std;
///提示:使用ctrl+shift+c可以给选定区域加注释附,使用Ctrl+shift+x可以取消注释。
///温馨提示:LOL中使用F可以出现人物特殊效果,Alt+F4可以开启神秘商店。。
int main(){
    ///应用1 --> 使用文件流输出数据到txt,并且从txt读入数据
//    ofstream out("c:\\text.txt",ios::out);
//    out<<"1234 pi= 3.14 !"<<endl;
//    out.close();
//    fstream in("c;\\text.txt",ios::in);
//    in.open("c:\\text.txt",ios::in);
//    int id; char str[10]; double pi; char fh;
//    in>>id>>str>>pi>>fh;
//    cout<<id<<endl<<str<<pi<<endl<<fh<<endl;
//    in.close();
    ///应用2 --> 将txt中的数据输出(仅限一行)
    ///get用来读取一个字符
//    ifstream read("c:\\a.txt",ios::in);
//    char now;
//    while((now=read.get()) != EOF){
//
//        cout<<now;
//    }
//    cout<<endl;
//    read.close();
    ///应用3 --> 二进制文件操作(文件加密)
//    ofstream out("c:\\out.txt",ios::binary);
//    int a[10] = {0,1,2,3,4,5,6,7,8,9};
//    out.write((char*)&a[0],sizeof(a));///转换为二进制形式,格式乱码
//    out.close();
//
//    int b[20];
//    ifstream in("c:\\out.txt",ios::binary);
//    in.read((char*)&b[0],sizeof(b));///快速读入功能,以字节数做区分,完全还原映像
//    for(int i = 0;i < 10;i++){
//        cout<<b[i]<<" ";
//    }
//    cout<<endl;
//    in.close();
//    return 0;
}

 

posted on 2016-06-28 15:59  icode-xiaohu  阅读(578)  评论(1编辑  收藏  举报