您的位置 首页 > 数码极客

mfc如何使用cout

用CPP开发widows应用有5种读写文件的方式,可以分别利用C语言函数,C++类、Win32API函数或MFC的Cfile类或CStdioFile来读写文件:

首先了解一个编程的概念,一是编程通常都会用到函数库或类库,二是编程通常都是某个操作系统平台上的编程。

类的成员方法的本质是隶属于类的函数。函数的接口是函数定义都需要特别关注的,是函数使用者需要了解的,一个函数定义好后,函数使用者调用时需要了解其主要可以达到的功能,返回的是什么类型的值,需要提供的参数。函数的接口就是函数定义者与使用者能够交流的,所需共同面对的一个部分。API是Application Programming Interface的缩写,也是一个接口的概念,说白了也是预先给你一堆定义好的函数,你只要了解各函数的接口,拿过来就可以用。Win32Api自然是用来编写Windows32应用的一堆函数。MFC是 Microsoft Foundation Classes的缩写,是用类封装了的Win32API。

1 C语言的CRT库提供了一级操作文件的函数 1.1 fOpen()函数(文件打开) 1.2 fread()函数(读) 1.3 fwrite()函数(写) 1.4 fClose()函数(文件关闭) 2 C++提供了不同功能的类来支持文件的输入输出 2.1 ofsteam类(从内存写到硬盘) 2.2 ifstream类(是从硬盘读到内存) 2.3 fstream类(读写) 3 VC++提供了Win32 API函数来操作文件 3.1 CreateFile(); 3.2 WriteFile(); 4 VC++的MFC提供的CFile 4.1 .Read()方法; 4.2 .Write()方法; 4.3 .Close()方法; 5 VC++的MFC提供的CStdioFile

1 C语言的标准库库提供了一组操作文件的函数

#include <;

#include <windows.h>

int main()

{

char m_fname[] = ";;

//写

FILE *fp;

if((fp=fopen(m_fname,"a+"))==NULL)

{

printf("\n 文件不能打开!\n");

exit(0);

}

fputs("writing",fp);

fclose(fp);

//读

FILE *stream;

char line[5];

if( (stream = fopen(m_fname, "r" )) != NULL )

{

while(fgets(line,5,stream)!=NULL)

{

printf( "%s", line);

}

printf("\n");

fclose( stream );

}

system("pause");

return 0;

}

2 C++提供了不同功能的类来支持文件的输入输出

#include <iostream> #include <fstream> using namespace std; int main() { //写 fstream file(";,ios::out); if(!()) { cout<<"start writing……"<<endl; file<<"name"<<" "; file<<"sex"<<" "; file<<"age"<<endl; } else cout<<"can not open"<<endl; (); cout<<" writed!"<<endl; //读 ifstream in(";); char buffer[200]; i()) { while(!in.eof()) { in.getline(buffer,100); cout<<buffer<<endl; in.close(); } } system("pause"); return 0; }

3 VC++提供了Win32 API函数来操作文件

//需要在同目录下 先保存有文件;

4 VC++的MFC提供的CFile类

微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发人员的工作量。

CFile采用API函数做函数内核,比如Open函数内部采用CreateFile作为打开文件方法。

编译时如果出错,需要进行如下设置:

“Project”菜单→“Setting”菜单项→General选项卡:

在Microsoft Fountion Classes:中选择:Use MFC in Static Library:

5 VC++的MFC提供的CStdioFile

CStdioFile类封装了C++运行时刻文件流的操作,流文件采用缓冲方式,支持文本模式(默认)和二进制模式文件操作。CStdioFile类从CFile类继承。

#include <a; int main() { CStdioFile file; (";,CFile::modeCreate|CFile::modeWrite); CString str; ("%s\r\n","hello!I am talking!"); (0,CFile::end); ( str ); (); return 0; }

附代码1

//VC++提供了Win32 API函数来操作文件 //需要在同目录下 先保存有文件; #include <; #include <windows.h> int main() { HANDLE hFILE=CreateFile("\ ;,GENERIC_WRITE,FILE_SHARE_READ,NULL,\ OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if(hFILE==INVALID_HANDLE_VALUE) { printf("CreateFile error\n"); return 0; } if(SetFilePointer(hFILE,0,NULL,FILE_END)==-1) { printf("SetFilePointer error\n"); return 0; } char buff[256]="hello"; DWORD dwWrite; if(!WriteFile(hFILE,&buff,strlen(buff),&dwWrite,NULL)) { printf("WriteFile error\n"); return 0; } printf("write %d.\n",dwWrite); printf("done.\n"); CloseHandle(hFILE); system("pause"); return 0; }

附代码2:

//CFile类 #include <a; #include <; #include <iostream> using namespace std; int main() { //读文件 CFile file_1; (";,CFile::modeNoTruncate | \ CFile::modeCreate | CFile::modeRead);//打开文件 int nlen = ();//获取文件总长度 char GetStr[4096] = {0}; (2,CFile::begin);//数据位置跳到第2个字节 (GetStr,8); ();//关闭文件 printf(GetStr); //写文件 CFile file_2; (";,CFile::modeNoTruncate | \ CFile::modeCreate | CFile::modeWrite);//打开文件 int nlen2 = ();//获取文件总长度 char GetStr2[4096] = {0}; memcpy(GetStr2,"123yui",6); ();//数据位置跳到文件末端 (GetStr2,6);//这里写了6个字节 ();//关闭文件 system("pause"); return 0; }

-End-

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“mfc如何使用cout,Mfc如何使用resizing,如何使用MFC,MFC如何打印C5”边界阅读