当前位置:   article > 正文

2023.12.26 c++文件读写操作 fileoption_c++写文件操作

c++写文件操作

fstream提供了三个类,用来实现c++对文件的操作(文件的创建、读、写)

     ifstream -- 从已有的文件读入

   ofstream -- 向文件写内容

   fstream - 打开文件供读写
  • 1
  • 2
  • 3
  • 4
  • 5

文件打开模式:

 ios::in             只读

 ios::out            只写

 ios::app            从文件末尾开始写,防止丢失文件中原来就有的内容

 ios::binary         二进制模式

 ios::nocreate       打开一个文件时,如果文件不存在,不创建文件

 ios::noreplace      打开一个文件时,如果文件不存在,创建该文件

 ios::trunc          打开一个文件,然后清空内容

 ios::ate            打开一个文件时,将位置移动到文件尾
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

文件指针位置在c++中的用法:

 ios::beg   文件头

 ios::end   文件尾

 ios::cur   当前位置
  • 1
  • 2
  • 3
  • 4
  • 5

常用的错误判断方法:

good()   如果文件打开成功

bad()   打开文件时发生错误

eof()    到达文件尾
  • 1
  • 2
  • 3
  • 4
  • 5

文件写入

//用来测试文件的读写操作,输入输出文件均为保存在程序根目录下的txt文件
void traj_out(const char* path,vector<vector<double>> &out_vector, int dof)
{
    // cout<<"******************************************"<<endl;
    cout<<"存储文件 -->"<<path << endl;

    ofstream	outfile;//outfile也是一个文件流,用来操作输出文件

    outfile.open(path,ios::trunc);  //打开输出文件
    for (vector<double>::size_type i=0;i!=(out_vector.size());i++)
    {
        for(int j=0;j<dof;j++)
        {
            outfile<< out_vector[i][j] << " " ;
        }
        outfile <<endl;
    }
    outfile.close();		//关闭输出文件
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

文件读取

//用来测试文件的读写操作,输入输出文件均为保存在程序根目录下的txt文件
vector<vector<double>> traj_in(string *path, int dof)
{
    ifstream	infile;	//infile是一个文件流,因此其实还是对流进行的操作
    vector<vector<double>> data_vector;	//用来保存文本中数据的向量
    infile.open(*path);  //打开输出文件

    string indata;
    double  temp;

    if(!infile)     //判断是否存在ifstream infile
    {
        cout<<"读入文件 : " << *path << " 不存在"<<endl;
    }

    if (infile.is_open())   //判断文件流是否处于打开状态
    {
        int line = 1;
        while (infile.good()&&!infile.eof())
        {
            vector<double> group;
//            cout << " line : " << line << endl;
            for(int i =0;i<dof;i++)
            {
                if(infile.good()&&!infile.eof())
                {
                    infile>>indata;
                    istringstream strstream(indata);
                    strstream>>temp;
//                    cout << temp <<endl;
                }
                else
                {
//                    cout<<"read file finished " << endl;
                    break;
                }
                group.push_back(temp);
            }
            line = line+1;
            if(group.size()==dof)
            {
                data_vector.push_back(group);
               //将数据读入到data_vector
            }
            else break;
        }
    }
    infile.close();
    cout <<  " 读取完成。 size : " <<   data_vector.size()  << endl;

    return data_vector;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/222904
推荐阅读
相关标签
  

闽ICP备14008679号