文件的操作

打开和关闭文件

  • open(const char* filename, ios_base::openmode mode):打开名为filename的文件,mode指定打开模式(如ios::inios::outios::binary等)。
  • close():关闭关联的文件。

写入文件

  • operator<<:重载的插入运算符,用于向文件中写入数据。
  • write(const char* s, streamsize n):以二进制形式向文件写入数据,s是指向数据的指针,n是要写入的字节数。

从文件读取

  • operator>>:重载的提取运算符,用于从文件中读取数据。
  • read(char* s, streamsize n):以二进制形式从文件读取数据,s是指向存放数据的缓冲区的指针,n是要读取的字节数。

文件指针操作

  • seekg(streampos pos):将输入文件指针移动到指定的位置pos
  • seekg(streamoff off, ios_base::seekdir dir):将输入文件指针根据偏移量off和方向dir移动,dir可以是ios::beg(文件开头)、ios::cur(当前位置)、ios::end(文件结尾)。
  • seekp(streampos pos):将输出文件指针移动到指定的位置pos
  • seekp(streamoff off, ios_base::seekdir dir):将输出文件指针根据偏移量off和方向dir移动。
  • tellg():返回当前输入文件指针的位置。
  • tellp():返回当前输出文件指针的位置。

文件状态检查

  • eof():检查是否到达文件末尾。
  • fail():检查上一次输入或输出操作是否失败。
  • good():检查流的状态,如果流状态良好,即之前的操作都成功,则返回true
  • bad():检查流是否处于错误状态。

//csv文件读写
#include<bits/stdc++.h>
using namespace std;

int main(){

    ofstream ofs;
    ofs.open("test.csv",ios::out);

    cout<<"csv文件的写入"<<endl;
    ofs<<"年龄"<<","<<"性别"<<endl;
    ofs<<"曾凡博"<<","<<"男"<<endl;
    ofs<<"zeng"<<","<<"女"<<endl;

    ofs.close();

    //csv文件的读
    ifstream ifs;
    ifs.open("test.csv",ios::in);

    if(!ifs.is_open()){
        cout<<"文件打开失败"<<endl;
    }

    string line;
    while(getline(ifs,line)){

        stringstream ss(line);
        string data;
        vector<string> rowData;

        while(getline(ss,data,',')){
            rowData.push_back(data);
        }
//        for(vector<string>::iterator ite=rowData.begin();ite!=rowData.end();ite++){
//            cout<<*ite<<" ";
//        }
        for(auto it:rowData){
            cout<<it<<" ";
        }
        cout<<endl;
    }
    ifs.close();
}
//文本文件读写
#include <iostream>
#include<fstream>
using namespace std;

int main()
{
    //定义输出流对象
    ofstream of;
    //
    of.open("test.txt",ios::out);

    of<<"张三"<<endl;
    of<<"18"<<endl;
    of<<"男"<<endl;

    of.close();

    //定义输入流
    ifstream ifs;

    ifs.open("test.txt",ios::in);
    if(!ifs.is_open()){
        cout<<"文件打开失败"<<endl;
        return 0;
    }

    //读数据

    //1
//    char buf[1024]={0};
//    while(ifs>>buf){
//        cout<<buf<<endl;
//    }
    //2
//    char buf[1024]={0};
//    while(ifs.getline(buf,sizeof(buf))){
//        cout<<buf<<endl;
//    }
    //3
//    string buf;
//    while(getline(ifs,buf)){
//        cout<<buf<<endl;
//    }
    //4
    char c;
    while((c=ifs.get())!=EOF){
        cout<<c;
    }
    ifs.close();
}
//二进制文件
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

class person{
public:
    //最好用string
    person(){}
    person(char * ch,int age){
        strcpy(m_Name,ch);

        m_Age=age;
    }
    char m_Name[60];
    int m_Age;
};

void test01(){

    ofstream ofs;
    ofs.open("person.txt",ios::out|ios::binary);

    person fanbo("张三",18);
    ofs.write(( char *)&fanbo,sizeof(person));
    ofs.close();
}
void test02(){

    ifstream ifs;
    ifs.open("person.txt",ios::in|ios::binary);

    if(!ifs.is_open()){
        cout<<"打开失败"<<endl;
        return;
    }
    person p;

    ifs.read(( char *)&p,sizeof(p));
    cout<<p.m_Name<<endl;
    cout<<p.m_Age<<endl;
    ifs.close();
}
int main(){

    test01();
    test02();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值