c++ 文件读写

文本模式打开与二进制模式打开的区别

  如果以文本打开文件,写入字符的时候,遇到

读取文件到数组

char *srcMd5="0789fe2ec66707bc7f8050cfbab94c99";
char dstMd5[33]={0};
int num=0;
system("md5sum TestRaw.yuv > md5Check.txt");
ifstream infile("./md5Check.txt");
if(!infile){
anlog ("Unable to open infile");
exit(1);
}
while( num < 32 ){
infile >> dstMd5[num];
num++;
}
infile.close();
cout << "dstMd5=" << dstMd5 << endl;
ASSERT_EQ(*(srcMd5),*(dstMd5));

文件的写入与打开方式无关,只与写入函数有关 

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
        char fileName[] ="out.yuv"; //读入文件名
        char * buffer;
        long size;
        ifstream fin(fileName,ios::in|ios::binary|ios::ate);
        ofstream fout("fs.yuv",ios::out|ios::binary); //输出文件名
        size = fin.tellg();
                cout << fin.tellg() << endl;
        fin.seekg(0);
        buffer = new char[size];
        fin.read (buffer,size);
        fout.write(buffer,size);
        fin.close();
        fout.close();
        delete[] buffer;
        return 0;
}
View Code
#include <fstream>      // std::ifstream, std::ofstr
// Copy a file
#include <fstream>      // std::ifstream, std::ofstream
int main () {
  std::ifstream infile ("out.yuv",std::ifstream::binary);
  std::ofstream outfile ("1111111.yuv",std::ofstream::binary);
  // get size of file
  infile.seekg (0,infile.end);
  long size = infile.tellg();
  infile.seekg (0);
  // allocate memory for file content
  char* buffer = new char[size];
  // read content of infile
  infile.read (buffer,size);
  // write to outfile
  outfile.write (buffer,size);
  // release dynamically-allocated memory
  delete[] buffer;
  outfile.close();
  infile.close();
  return 0;
}
View Code

    

http://blog.chinaunix.net/uid-21375345-id-3049692.html

原文地址:https://www.cnblogs.com/Malphite/p/7495762.html