文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > MFC 文件操作
侵权投诉

MFC 文件操作

别:
  写入文件时:
ofstream f("c:\\1.txt");
char buf[3];
buf[0]='a';
buf[1]='\n';
buf[2]='b';
f.write(buf,3);
读出文件时:
ifstream f("c:\\1.txt");
f.setmode(filebuf::binary);
char buf[5];
memset(buf,0,5);
f.read(buf,5);
CString str;
str.Format("%d,%d,%d,%d",buf[0],buf[1],buf[2],buf[3]);
MessageBox(str);
    在写入文件时不指定格式,文件将按文本格式存储,此时读出文件时指定二进制格式,读出的数据如下图:

如果注释f.setmode(filebuf::binary);语句,文件将按文本文件读出,如下图:

二、 注册表的操作
1. 读写win.ini文件:
使用API的GetProfileInt和WriteProfileString来实现一个保存窗口大小的例子。
在CMainFrame的
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();

// TODO: Add your message handler code here
CRect rect;
GetWindowRect(&rect);
CString str;
str.Format("%d",rect.Width());
WriteProfileString("窗口尺寸","宽度",str);
str.Format("%d",rect.Height());
WriteProfileString("窗口尺寸","高度",str);
}

在CMainFrame的
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.cx=GetProfileInt("窗口尺寸","宽度",100);
cs.cy=GetProfileInt("窗口尺寸","高度",100);
return TRUE;
}

演示使用API的GetProfileString.对于它的第四个参数lpReturnedString需要添一个char*来返回。这里不能添CString对象来返回,这是一个特殊的地方。别的函数一般要char*的时候都可以用CString对象来代替。
这里我们用CString的GetBuffer来添这个char*。
A CString object consists of a variable-length sequence of characters.
因为一个字符串对象由一序列长度可变的字符组成。

Returns a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.
返回一个CString对象内部字符的缓冲区(字符数组)的指针,这个返回的指针不是一个常量的指针,因而允许直接修改指针所指向的CString对象的内容。这个指针和CString内部字符数组的地址是相等的。

If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString member functions.
如果你使用这个通过GetBuffer返回的指针改变了字符串的内容,你在使用CString其他成员函数之前必须调用ReleaseBuffer。

在CWinApp的InitInstance里
CString str;
::GetProfileString("窗口尺寸","高度","无值",str.GetBuffer(100),100);
AfxMessageBox(str);
下面测试CWinApp的WriteProfileString,GetProfileString。
对于WriteProfileString有段说明
· In Windows NT, the value is stored to a registry key.
· In Windows 3.x, the value is stored in the WIN.INI

上一页第3页

你可能喜欢

热门文档

相关文档

  • MFC文件操作

    MFC 文件操作 1.文件的查找当对一个文件操作时,如果不知道该文件是否存在,就要首先进 行查找。MFC 中有一个专门用来进行文件查找的类 CFileFind,使用 它可以方便...

  • MFC文件操作大全

    MFC文件操作大全_计算机硬件及网络_IT/计算机_专业资料。实例:①遍历文件夹并且计算文件夹个数 int iCount = 0; CString strTempPath; CString strFileName; s...

  • MFC文件操作2

    (hFile);//关闭句柄 /*---用MFC的CFile类来操作文件--- CFile file("CFile.txt", //先构造一个CFile对象 CFile::modeCreate CFile::modeWrite); file...

  • MFC文件操作大全

    MFC文件操作大全_IT/计算机_专业资料。MFC文件操作大全1.创建文件夹 CreateDirectory(%%1,NULL); 2.创建文件 CFile file; file.Open(%%1,CFile::modeCreate...

  • mfc文件操作个人总结

    mfc文件操作个人总结_计算机软件及应用_IT/计算机_专业资料。个人总结的MFC有关文件操作,有例子MFC 文件操作 一,打开文件对话框 CString szFilters=_T("txt Files...

  • MFC文件操作

    MFC文件操作_IT/计算机_专业资料。文件操作:二进制文件和文本文件的区别。二进制文件将数据在内存中存在的模式原封不动的搬到文件中,而文本文件是将数据的asc码...

  • MFC中文件操作

    MFC中文件操作_电脑基础知识_IT/计算机_专业资料。C、C++、WinAPI、MFC等对文件的操作一、C 语言的文件操作通过 FILE 结构体实现。 1、文件的打开 C 语言中对...

  • MFC文件操作大全

    MFC文件操作大全 C++MFCC++MFC隐藏>> 1.创建文件夹 CreateDirectory(%%1,NULL); 2.创建文件 CFile file; file.Open(%%1,CFile::modeCreateCFile::mode...

  • Visual C++ MFC文件操作大全

    Visual C++ MFC文件操作大全_理学_高等教育_教育专区 暂无评价0人阅读0次下载举报文档 Visual C++ MFC文件操作大全_理学_高等教育_教育专区。Windows程序设计...

  • MFC所有的文件操作

    MFC所有的文件操作_互联网_IT/计算机_专业资料。MFC丰富的文件操作,基本包含了所有的文件操作。1.创建文件夹 CreateDirectory(%%1,NULL); 2.创建文件 CFile ...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email