文本模式打开与二进制模式打开的区别
如果以文本打开文件,写入字符的时候,遇到\n
读取文件到数组
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; }

#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; }
http://blog.chinaunix.net/uid-21375345-id-3049692.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理