文件读写笔记

时间:2022-09-04 19:23:33

1.今天学会了文件的读写,搞清楚了CString和string是的区别:

前者是MFC和ATL里定义的类,后者是标准c++里定义的类。

2.在用gcc编译c++源码时,如果用的c++库,需要在链接时显式说明,格式如下 gcc  ...(编译命令) target.cpp -lstdc++ -o target,否则会报错

报错诸如:undefined reference to "std::basic_ofsteam...

3.今天才理解,文件操作是标准c++提供的,好惭愧。。。。

4.ios::app和ios::ate的区别

ios::app 如果没有文件,创建文件,如果有文件,则定位到文件末尾。

ios::ate  如果没有文件,创建文件,如果有文件,则清空文件。


//方法一:用MFC编程可用CFile
#includevoid tiaoshi(char *p){int len = strlen(p);CFile *cf = new CFile(".\\log_Test.log",CFile::modeReadWrite);cf->SeekToEnd();cf->Write("\r\n", 2);cf->Write(p, len);cf->Flush();delete cf;}
//方法二:使用标准C++#include<fstream>void m_log(string c){//调试信息输出初始化ofstream out;out.open("/root/cpltest/log.txt",ios::app);out << c <<"\r\n";out.close();}