c语言可以用cpp吗(CPP五种文件读写方式)
c语言可以用cpp吗(CPP五种文件读写方式)char m_fname[] = "test1.txt";{#include <stdio.h>#include <windows.h>int main()
用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 <stdio.h>
#include <windows.h>
int main()
{
char m_fname[] = "test1.txt";
//写
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("test1.txt" ios::out); if(!file.fail()) { cout<<"start writing……"<<endl; file<<"name"<<" "; file<<"sex"<<" "; file<<"age"<<endl; } else cout<<"can not open"<<endl; file.close(); cout<<" writed!"<<endl; //读 ifstream in("test1.txt"); char buffer[200]; if(in.is_open()) { while(!in.eof()) { in.getline(buffer 100); cout<<buffer<<endl; in.close(); } } system("pause"); return 0; }
3 VC 提供了Win32 API函数来操作文件
//需要在同目录下 先保存有test1.txt文件;
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 <afx.h> int main() { CStdioFile file; file.Open("test1.txt" CFile::modeCreate|CFile::modeWrite); CString str; str.Format("%s\r\n" "hello!I am talking!"); file.Seek(0 CFile::end); file.WriteString( str ); file.Close(); return 0; }
附代码1
//VC 提供了Win32 API函数来操作文件 //需要在同目录下 先保存有test1.txt文件; #include <stdio.h> #include <windows.h> int main() { HANDLE hFILE=CreateFile("\ test1.txt" 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 <afx.h> #include <stdio.h> #include <iostream> using namespace std; int main() { //读文件 CFile file_1; file_1.Open("test1.txt" CFile::modeNoTruncate | \ CFile::modeCreate | CFile::modeRead);//打开文件 int nlen = file_1.GetLength();//获取文件总长度 char GetStr[4096] = {0}; file_1.Seek(2 CFile::begin);//数据位置跳到第2个字节 file_1.Read(GetStr 8); file_1.Close();//关闭文件 printf(GetStr); //写文件 CFile file_2; file_2.Open("test1.txt" CFile::modeNoTruncate | \ CFile::modeCreate | CFile::modeWrite);//打开文件 int nlen2 = file_2.GetLength();//获取文件总长度 char GetStr2[4096] = {0}; memcpy(GetStr2 "123yui" 6); file_2.SeekToEnd();//数据位置跳到文件末端 file_2.Write(GetStr2 6);//这里写了6个字节 file_2.Close();//关闭文件 system("pause"); return 0; }
-End-