MFC文件操作学习笔记

来源:互联网 发布:asp.net开源商城源码 编辑:程序博客网 时间:2024/04/28 00:33
<span style="white-space:pre"></span>FILE *pFile;<span style="white-space:pre"></span>fopen_s(&pFile, "1.txt", "w");// TODO: 在此添加命令处理程序代码<span style="white-space:pre"></span>fwrite("try best", 1, strlen("try best"), pFile);<span style="white-space:pre"></span>fflush(pFile);<span style="white-space:pre"></span>//fclose(pFile);
void CFileTestView::OnFileRead(){<span style="white-space:pre"></span>FILE *pFile;<span style="white-space:pre"></span>//TCHAR<span style="white-space:pre"></span><span style="white-space:pre"></span>fopen_s(&pFile, "1.txt", "r");// TODO: 在此添加命令处理程序代码<span style="white-space:pre"></span>fseek(pFile, 0, SEEK_END);//移动文件指针到结尾处<span style="white-space:pre"></span>int file_len = ftell(pFile);             //获得文件指针的位置<span style="white-space:pre"></span>char *Read_buffer;<span style="white-space:pre"></span>Read_buffer = new char[file_len + 1];//加1 最后一位置0<span style="white-space:pre"></span>//memset(Read_buffer, 0, 100); //<span style="white-space:pre"></span>Read_buffer[file_len] = 0;<span style="white-space:pre"></span>rewind(pFile);<span style="white-space:pre"></span>//fseek(pFile, 0, SEEK_SET);<span style="white-space:pre"></span>fread(Read_buffer, 1, file_len , pFile);<span style="white-space:pre"></span><span style="white-space:pre"></span>fclose(pFile);}

文件缓冲系统。使得文件不能立即更新内容。可以使用fflush

fseek设置写入位置

写入数据时使用offstream类

ofstream ofs("4.txt");ofs.write("china dota best dota", strlen("china dota best dota"));ofs.close();

需要添加<fstream.h> 并使用命名空间 using namespace std;

读取数据时使用ifstream类

ifstream ifs("4.txt");char buffer[100];memset(buffer, 0, 100);ifs.read(buffer, 100);ifs.close();
句柄方式:

HANDLE hFile;DWORD Bytes;hFile = CreateFile(_T("5.txt"), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);WriteFile(hFile, "China Dota Best Dota", strlen("China Dota Best Dota"), &Bytes, NULL);CloseHandle(hFile);
CFile方式:

CFile cFile(_T("6.txt"),CFile::modeWrite | CFile::modeCreate);cFile.Write("NewBee Dota is ti6 Champion", strlen("NewBee Dota is ti6 Champion"));cFile.Close();
<span style="white-space:pre"></span>CFile cFile(_T("6.txt"), CFile::modeRead);<span style="white-space:pre"></span>char buffer[100];<span style="white-space:pre"></span>memset(buffer, 0, 100);<span style="white-space:pre"></span>cFile.Read(buffer, 30);<span style="white-space:pre"></span>cFile.Close();

CFileDialog

CFileDialog cFileDlg(FALSE);  //另存为  TRUE为打开//cFileDlg.m_ofn.lpstrFileTitle = _T("我的文件保存对话框");cFileDlg.m_ofn.lpstrFilter = _T("TextFile(*.txt)\0*.txt\0AllFile(*.*)\0*.*\0\0");cFileDlg.m_ofn.lpstrDefExt = _T("txt");if(IDOK == cFileDlg.DoModal()){CString filename = cFileDlg.GetFileName();    CFile cFile(filename, CFile::modeWrite | CFile::modeCreate);cFile.Write("NewBee Dota is ti6 Champion", strlen("NewBee Dota is ti6 Champion"));cFile.Close();}<span style="white-space:pre"></span>CFileDialog cFileDlg(TRUE);  //另存为  TRUE为打开<span style="white-space:pre"></span>  //cFileDlg.m_ofn.lpstrFileTitle = _T("我的文件保存对话框");<span style="white-space:pre"></span>cFileDlg.m_ofn.lpstrFilter = _T("TextFile(*.txt)\0*.txt\0AllFile(*.*)\0*.*\0\0");<span style="white-space:pre"></span>//cFileDlg.m_ofn.lpstrDefExt = _T("txt");<span style="white-space:pre"></span>if (IDOK == cFileDlg.DoModal())<span style="white-space:pre"></span>{<span style="white-space:pre"></span>CString filename = cFileDlg.GetFileName();<span style="white-space:pre"></span>CFile cFile(filename, CFile::modeRead);<span style="white-space:pre"></span>char buffer[100];<span style="white-space:pre"></span>memset(buffer, 0, 100);<span style="white-space:pre"></span>cFile.Read(buffer, 30);<span style="white-space:pre"></span>cFile.Close();<span style="white-space:pre"></span>}







0 0
原创粉丝点击