keywords:C++, 读写二进制文件, 文本文件, iostream, write file, read file, write binary, read binary

ifstream,ofstream读写二进制文件
#include <iostream>  
#include <fstream>  
using namespace std;  
  
int main(int argc, char** argv)  
{  
  
  int a[5] = {1,2,3,4,5};  
  int b[5];  
  
  ofstream ouF;  
  ouF.open("./me.dat", std::ofstream::binary);  
  ouF.write(reinterpret_cast<const char*>(a), sizeof(int)*5);  
  ouF.close();  
  
  ifstream inF;  
  inF.open("./me.dat", std::ifstream::binary);  
  inF.read(reinterpret_cast<char*>(b), sizeof(int)*5);  
  inF.close();  
  
  for (int i = 0; i < 5; i++)  
  {  
    cout << b[i] << endl;  
  }  
  return 0;  
}

参考:
https://blog.csdn.net/bendanban/article/details/30039193

获取文件的大小:

ifstream inF;
inF.open("C:/test.jpg", ios::binary | ios::ate);

long size = inF.tellg();

当使用ios::binary | ios::ate之后,inF.read(reinterpret_cast<char*>(b), sizeof(int)*5);就会失效。

Reference:
https://stackoverflow.com/questions/2409504/using-c-filestreams-fstream-how-can-you-determine-the-size-of-a-file

C模式读写二进制文件
//采用C模式写二进制文件  
void DataWrite_CMode()  
{  
    //准备数据  
    double pos[200];  
    for(int i = 0; i < 200; i ++ )  
        pos[i] = i ;  
    //写出数据  
    FILE *fid;  
    fid = fopen("binary.dat","wb");  
    if(fid == NULL)  
    {  
        printf("写出文件出错");  
        return;  
    }  
    int mode = 1;  
    printf("mode为1,逐个写入;mode为2,逐行写入\n");  
    scanf("%d",&mode);  
    if(1==mode)  
    {  
        for(int i = 0; i < 200; i++)  
            fwrite(&pos[i],sizeof(double),1,fid);  
    }  
    else if(2 == mode)  
    {  
        fwrite(pos, sizeof(double), 200, fid);  
    }  
    fclose(fid);  
}

//采用C模式读二进制文件  
void DataRead_CMode()  
{  
    FILE *fid;  
    fid = fopen("binary.dat","rb");  
    if(fid == NULL)  
    {  
        printf("读取文件出错");  
        return;  
    }  
    int mode = 1;  
    printf("mode为1,知道pos有多少个;mode为2,不知道pos有多少个\n");  
    scanf("%d",&mode);  
    if(1 == mode)  
    {  
        double pos[200];  
        fread(pos,sizeof(double),200,fid);  
        for(int i = 0; i < 200; i++)  
            printf("%lf\n", pos[i]);  
        free(pos);  
    }  
    else if(2 == mode)  
    {  
        //获取文件大小  
        fseek (fid , 0 , SEEK_END);         
        long lSize = ftell (fid);    
        rewind (fid);   
        //开辟存储空间  
        int num = lSize/sizeof(double);  
        double *pos = (double*) malloc (sizeof(double)*num);    
        if (pos == NULL)    
        {    
            printf("开辟空间出错");     
            return;   
        }   
        fread(pos,sizeof(double),num,fid);  
        for(int i = 0; i < num; i++)  
            printf("%lf\n", pos[i]);  
        free(pos);     //释放内存  
    }  
    fclose(fid);  
}

参考:
https://blog.csdn.net/nichengwuxiao/article/details/78789225

ifstream,ofstream读写文本文件
// writing on a text file
#include <fiostream.h>

int main () 
{
    ofstream examplefile   ("example.txt");
    if (examplefile.is_open()) 
    {
        examplefile <<   "This is a line.\n";
        examplefile <<   "This is another line.\n";
        examplefile.close();
    }
    return 0;
}

// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main () 
{
    char buffer[256];
    ifstream examplefile   ("example.txt");
    if (! examplefile.is_open())
    {
        cout << "Error   opening file"; exit (1); 
    }
    while (! examplefile.eof() ) 
    {
        examplefile.getline   (buffer,100);
        cout << buffer   << endl;
    }
    return 0;
}

参考:
http://www.cnblogs.com/azraelly/archive/2012/04/14/2446914.html

C模式读写文本文件
#include <stdio.h>  

int main()  
{  
    //下面是写数据,将数字0~9写入到data.txt文件中  
    FILE *fpWrite=fopen("data.txt","w");  
    if(!fpWrite)  
    {  
        return 0;  
    }  
    for(int i=0;i<10;i++)  
        fprintf(fpWrite,"%d ",i);  
    fclose(fpWrite);  
    //下面是读数据,将读到的数据存到数组a[10]中,并且打印到控制台上  
    int a[10]={0};  
    FILE *fpRead=fopen("data.txt","r");  
    if(!fpRead)  
    {  
        return 0;  
    }  
    for(int i=0;i<10;i++)  
    {  
        fscanf(fpRead,"%d ",&a[i]);  
        printf("%d ",a[i]);  
    }  
    getchar();//等待  
  
    return 1;  
}

参考:
https://blog.csdn.net/hjl240/article/details/47132477

Check if a file exists

C 98

#include <sys/stat.h>

inline bool exists (const std::string& filename) {
  struct stat buffer;   
  return (stat (filename.c_str(), &buffer) == 0); 
}

C++ 17:

#include <filesystem>

bool exist = std::filesystem::exists(filename);

Origin:
https://stackoverflow.com/a/51301928/1645289

combine file path string
#include <filesystem> //C++ 17
#include <iostream>

namespace fs = std::filesystem;
using namespace std;

void main()
{
    std::filesystem::path work_dir("c:\\temp");
    std::string full_path = work_dir / "my_file.txt";
    cout << full_path << endl;
}

Output: c:/temp/my_file.txt

Origin: c++ combine path of file as a string with file name
https://stackoverflow.com/a/68504328/1645289


为天地立心,为生民立命,为往圣继绝学,为万世开太平。 ----北宋·张载