MFC文件操作学习笔记
2016-07-07 10:55
375 查看
<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);}文件缓冲系统。使得文件不能立即更新内容。可以使用fflushfseek设置写入位置写入数据时使用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();CFileDialogCFileDialog 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>}
相关文章推荐
- js history使用范例
- Java Script 函数基础知识
- python复习
- Java中的Class类
- JHTP自测题_第七章_数组及动态数组(Array and ArrayList)
- yarn
- Entity Framework Tutorial Basics(36):Eager Loading
- 配置jdk
- 你知道你为什么穷吗?因为你喜欢省钱!
- 基于iscroll实现下拉和上拉刷新
- 【小知识学习】(Secure socket layer)SSL连接
- Android 面试精华题目总结
- iOS学习(十)Objective-C 内存管理规则
- eclipse中maven项目jar包不在同一个目录下
- ShaderForge
- HTML
- poj 1065 Wooden Sticks贪心水过
- table关键字用法
- session入mysql
- 下游系统访问思考