您的位置:首页 > 其它

文件读写的操作整理

2014-07-13 17:50 357 查看
一.写入文件

由于文件操作平时经常要用到,所以将一些常用的文件操作整理如下,其中包括C语言中文件读写的应用,c++中文件读写的应用,SDK中文件读写的应用和MFC中文件读写的应用。

C语言中的写入

FILE *pFile=fopen("1.text","w");

fwrite("wenjianduxie“,1,strlen(wenjianduxie),pFile);

fclose(pFile);//关闭 把文件写入缓冲区

fflush(pFile);//刷新缓冲区,立即更新

移动到开头写入数据

移动指针

fseek()函数实现

fseek(pFile,0,SEEK_SET);

方法2:

FILE *pFile=fopen("2.text","w");

char ch[3];

ch[0]='a';

ch[1]=10;

ch[2]='b';

fweite(ch,1,3,pFile);

fclose(pFile);

//产生的文件多了一个字节

打开方式中加"b"将按二进制打开

C++中的写入

ofstream ofs("4.text");

ofs.weite("wenjianduxie",strlen("wenjianduxie"));

ofs.close();

//需要包含头文件:#include "fstream.h"

SDK中对文件的写入打开

CreateFile类 返回句柄

参数(文件名 ,访问方式,共享方式,指向结构的指针,如何创建,设置文件属性,指定模板文件句柄)

HANDLE hFile;

hFile=CreateFile("5.text",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);

DWORD dvWrites;

WriteFile(hfile,"wenjianduxie",strlen("wenjianduxie"),&dvWrites,NULL);

CloseHandle(hFile);

MFC中对文件的写

CFile file("6.text",CFile::modeCreate|CFile::modeWrite);

file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));

file.Close();

创建一个写入文件对话框

使用类 CFileDialog

CFileDialog filedlg(FALSE);

filedlg.m_ofm.IpstrTile="我的文件";//改变标题

filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器

filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名

if (IDOK==filedlg.DoModal());

{

CFile file(filedlg.GetfileName(),CFile::modeCreate|CFile::modeWrite);

file.Write("http://sina.com.cn",strlen("http://sina.com.cn"));

file.Close();

}

初始化程序的写入 对INI文件的写(针对16位的操作系统在32位操作系统中因该对注册表进行读写)

使用类WriteProfileString

::WriteProfileString("段名","keyname","string");

二.读取文件

C语言中的读取

FILE *pFile=fopen("1.text","r")

char ch[100];

/*

menset(ch,0,100);//全部设为0

*/

fread(ch,1,100,pFile);//通过写入数据是把strlem()+1实现加上/0

MessageBox(ch);

fclose(pFile);

/*

ftell(FILE *stream)//得到文件当前的位置

*/

char *pBuf;

fseek(pFile,0,SEEK_END);

int len=ftell(pFile);

pBuf=new char[len+1];

rewind(pFile);//指针移到文件头

fread(pBuf,1,len,pFile);

pBuf[len]=0;

MessageBox(pBuf);

//得到文件长度输出数据

方法2:

FILE *pFile=fopen("2.text","r");

char ch[100];

fread(ch,1,3,pFile);

ch[3]=0;

MessageBox(ch);

fclose(pFile);

用二进制方式读时 字节和文本方式读不同

例子:保存整形数据98241在文本文件中,当打开文件时文本显示为98241

分析:文本保存的是ASC2码的数据,不是一个数值,一个int占四个字节

程序:

FILE *pFile=fopen("3.text","w");

int i=98341;

fwrite(&i,4,1,pFile);

fclose(pFile);

以上程序打开的是乱码

0 -- 48 ASC

FILE *pFile=fopen("3.text","w");

int i=98341;

char ch[5];

ch[0]=9+48;

ch[1]=8+48;

ch[2]=3+48;

ch[3]=4+48;

ch[4]=1+48;

/*简单方式

itoa(i,ch,10);

fwrite(ch,1,5,pFile);

fclose(pFile);

C++中的读出

使用ifstream类

ifstream ifs("4.text")

char ch[100];

menset(ch,0,100);

ifs.read(ch,100);

ifs.close();

MessageBox(ch);

SDK中对文件的打开读写

HANDLE hFile;

hFile=CreateFile("5.text",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

char ch[100];

DWORD dvReads;

ReadFile(hFile,cyh,100,&dvReads,NULL);

ch[dvReads]=0;

CloseHandle(hFile);

MessageBoxe(ch);

MFC中对文件的读

CFile file("6.text",CFile::modeRead);

char *pBuf;

DWORD dvFile;

deFile=file.GetLength();

pBuf=new char[dvFile+1];

pBuf[dvFile]=0;

file.Read(pBuf,dufile);

file.close();

创建一个读出文件对话框

使用类 CFileDialog

CFileDialog filedlg(FALSE);

filedlg.m_ofm.IpstrTile="我的文件";//改变标题

filedlg.m_ofm.Ipstrfilter="Text files(*.text)/0*.text/0ALL Files(*.*)/0*.*/0/0";//设置过滤器

filedlg.m_ofm.IpstrDefExt="tet";//添加缺省扩展名

if (IDOK==filedlg.DoModal());

{

CFile file(filedlg.GetfileName(),CFile::modeRead);

char *pBuf;

DWORD dvFile;

deFile=file.GetLength();

pBuf=new char[dvFile+1];

pBuf[dvFile]=0;

file.Read(pBuf,dufile);

file.close();

MessageBox(pBuf)

}

初始化程序的读 对INI文件的读(针对16位的操作系统在32位操作系统中因该对注册表进行读写)

使用类GetProfileString

CString str;

::GetProfileString("段名","keyname","缺省字符串",str.GetBuffer(100),100);

AFXMessageBox(str);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: