c++—文件编程

1. c++对文件的操作是由文件流类完成的,文件流类在流类与文件间建立连接;

2. 文件流类型

        ①文件输入流:ifstream

        ②文件输出流:ofstream

        ③文件输入/输出流:fstream

3. 语法

        ①定义文件流类的对象

ifstream ifile;   //定义了一个文件输入流对象;
ofstream ofile;   //定义了一个文件输出流对象;
fstream iofile;   //定义了一个文件输出/输入流对象;

        ②文件的打开方式

ifstream → ios::in
ofstream → ios::out|ios::trunc
fstream  → ios::in|ios::out|ios::app  

        ③文件打开示例

ifstream ifs("xxx.txt", ios::in);
if(!ifs)
{
    cout<<"open error ifs"<<endl;
}

ofstream ofs("yyy.txt",ios::out|ios::app)
if(!ofs)
{
    cout<<"open error ofs"<<endl;
}

fstream fs("zzz.txt",ios::in|ios::out|ios::trunc)
if(!fs)
{
    cout<<"open fs error"<<endl;
}

4. 文件中get()与getline()两个成员函数之间的区别

        ①get()遇到设置的终止符会停止,且不会从流中提取终止符,也不会越过终止符;

        ②getline()则会越过终止符(例如‘\n’),但是仍然不会把它放到缓冲区,比如在文件拷贝过程中设置了终止符是'\n',那么则需要在每次写入后在写入一个‘\n’;

5. 其他一些函数

        ①ignore():用于跳过流中的n个字符,或者遇到结束字符位置;

        ②int peek():窥视最近一次移动的指针,即向后看一下下一个字符是什么;实际并不改变文件读写位置;

        ③putback(char c):将字符c插入到当前的指针位置;

        ④close():成员函数,用来关闭文件;

 6. 随机读写函数,即随机移动文件指针,下文有示例

tellg()         //返回当前指针位置,输入流操作
seekg(绝对位置)  //输入数值,进行绝对移动
seekg(相对位置,参照位置)  //相对于参照位置进行移动
seekp(绝对位置)  //输入数值,进行绝对移动
seekp(相对位置,参照位置)  //相对于参照位置进行移动
tellp();        //返回当前指针位置,输出流操作

//参照位置的三种方式:
ios::beg  //相对于文件头
ios::cur  //相对于当前位置
ios::end  //相对于文件尾
#include <iostream>
#include <fstream>
#include <cassert>

using namespace std;

int main(int argc, char **argv)
{
    fstream fs(argv[1], ios::in | ios::out | ios::trunc);
    assert(fs);
    char buf[100];
    fs<<"aabbccdd";
    fs.seekg(0,ios::beg);  //将文件指针定位在开头

    fs>>buf;

    cout<<"buf : "<<buf<<endl;

    return 0;
}

7. 文件拷贝程序,两个版本,分别是file.get()、file.getline();

file.get():

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>

using namespace std;

int main(int argc, char ** argv)
{
    fstream from_file(argv[1],ios::in);
    if(!from_file)
    {
        cout<<"open from_file failed"<<endl;
        exit(-1);
    }

    fstream to_file(argv[2],ios::out|ios::trunc);
    if(!to_file)
    {
        cout<<"open to_file failed"<<endl;
        exit(-1);
    }

    char buf[1024];

    while(!from_file.eof())
    {
        memset(buf,0,sizeof(buf));
        from_file.get(buf,1024,'\n');
        to_file.write(buf,strlen(buf));
        while(from_file.peek() == '\n')  //因为get()不会跳过终止符
        {
            to_file << '\n';
            from_file.ignore();
        }
    }

    from_file.close();
    to_file.close();

    return 0;
}

file.getline():

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <cassert>

using namespace std;

int main(int argc, char **argv)
{
    ifstream in_file(argv[1],ios::in);
    assert(in_file);

    ofstream out_file(argv[2],ios::out|ios::trunc);
    assert(out_file);

    char buf[1024];

    while(!(in_file.eof()))  //file.eof代表文件尾
    {
        memset(buf,0,sizeof(buf));
        in_file.getline(buf,sizeof(buf),'\n');  //getline会跳过终止符
        out_file.write(buf,strlen(buf));
        out_file<<'\n';  //每次补充一个\n进去;

    }

    in_file.close();
    out_file.close();

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值