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
- MFC文件操作学习笔记
- [MFC学习笔记]--lesson12 文件操作
- MFC学习笔记之:MFC文件、文件夹操作完全整理
- MFC学习笔记之XML 文件操作类
- VS2013学习笔记之MFC框架CFile类文件操作
- MFC 文件操作概要 笔记
- MFC文件操作笔记整理
- MFC学习笔记-操作篇
- [学习笔记] 文件操作
- 菜鸟笔记之MFC文件操作
- MFC学习(23)MFC 文件操作详解
- MFC学习笔记--打开文件对话框
- delphi学习笔记--文件操作
- Ubuntu_文件操作学习笔记
- Perl文件操作学习笔记
- C#学习笔记:文件操作
- java文件操作学习笔记
- Python学习笔记--文件操作
- iOS学习(十)Objective-C 内存管理规则
- .net乱码问题
- 基于iscroll实现下拉和上拉刷新
- MinGW下的单步调试
- js history使用范例
- MFC文件操作学习笔记
- CocoaPods版本升级
- 继承,多态使用
- javascript基础:事件1
- yii框架中文件上传
- android常用工具类库整理
- KK版本和L版本编绎camera参数命
- C/C++中的玄机
- SpringMVC整合Mongodb开发,高级操作