c语言方式
#include <fstream> //文件读写的头文件 using namespace std; void 写到文件(char* path, char* str){ //需要写 #include <fstream> 和 using namespace std; std::ofstream ofs; ofs.open(path,ios::out); ofs << str; ofs.close(); }
window
bool 写到文件(const TCHAR* 文件路径, const TCHAR* 要写入的内容) { //直接打开 HANDLE hFile = CreateFile(文件路径, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (INVALID_HANDLE_VALUE != hFile) { //将数据转成Ansi(当然也可以直接unicode) //SetFilePointer(hFile, 0, 0, FILE_END); DWORD len = WideCharToMultiByte(CP_ACP, NULL, 要写入的内容, -1, NULL, NULL, NULL, FALSE); if (len <= 0) return false; char* pBuffer = new char[len]; WideCharToMultiByte(CP_ACP, NULL, 要写入的内容, -1, pBuffer, len, NULL, FALSE); DWORD dwWrite = 0; int size = strlen(pBuffer); WriteFile(hFile, pBuffer, strlen(pBuffer), &dwWrite, NULL); delete[] pBuffer; CloseHandle(hFile); return true; } return false; }