来一段简单的C++打开指定文件的代码谢谢

来源:百度知道 编辑:UC知道 时间:2024/09/28 17:26:01

要简单的就用c++的流好了.

#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream ifs("ReadMe.txt"); //要打开的文件
if (!ifs.bad()) //判断是否成功
{
char buf[1000];
ifs.read(buf, 1000); //从流里读, 执行完毕之后buf里就是文件的内容.
buf[ifs.gcount()+1] = '\0'; //规范字符串
ifs.close(); //关闭流
cout << buf; //打印buf内容
}
//system("pause");
}

7.读取文件
char sRead[1024];
CFile mFile(_T(%%1),CFile::modeRead);
while (sRead!=null)
{
mFile.Read(sRead,1024);
CString %%2=CString(sRead);
%%3
}

8.写入文件
CFile mFile(_T(%%1), CFile::modeWrite|CFile::modeCreate);
mFile.Write(%%2);
mFile.Flush();
mFile.Close();

9.写入随机文件
char szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];
GetTempPath(_MAX_PATH, szTempPath);
GetTempFileNa