您的位置:首页 > 编程语言 > C语言/C++

VC++学习(12):文件操作

2011-07-28 17:21 281 查看
1. 常量指针与指针常量的区分
char ch[5]="lisi";
const char *pStr=ch;//const在*之前,表明指针指向的内容为常量,即为常量指针,但指针可指向其它变量。
char * const pStr=ch;//const在*之后,表明指针的地址不能改变,即为指针常量,但指针所指向的内容是可以改变的;

const char * const pStr = ch;指向常量的常量指针,指针的地址与指向的内容都不可以改变。

2. 对文件读写的三种方法

<1>.C语言

FILE *pFile=fopen("1.txt","w");//参数1文件路径,只写文件则在本工程中,参数2:打开模式

fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);//写文件

原型:size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream);

在写完文件后要使用fflush(pFile);或fclose(pFile)使数据写入到文件中,因为C语言对文

件的操作使用了缓冲文件系统,一般如果不手工刷新缓冲区的话,直到缓冲区满后才将数据写入到文件中

int fseek(FILE *stream, long offset, int origin);

stream 指向FILE结构体的指针

offset 设定偏移量

origin 指定文件指针的起始位置.( SEEK_SET开始处, SEEK_CUR文件当前位置处, SEEK_END文件的结尾处)

//fseek(pFile,0,SEEK_SET);

对于C语言文件操作来说 ,它有一个文件指针,该指针会随时根据我们对文件的操作来移动地,始终指向文件下一个写入的位置.当执行定稿操作之后,文件指针就指向了所写数据占据位置的下一个位置.如果希望在写入数据后,返回到文件的开始位置处再写入数据,就需要将这个文件指针移动到文件开始校园,这可以利用 fseek函数实现

//fwrite("ftp:",1,strlen("ftp:"),pFile);

//fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);

char ch[100];接收文件中数据字符数据

memset(ch, 0, strlen(ch));

fread(ch, 1, 100, pFile);

fclose(pFile);*/关闭文件

//fflush(pFile);刷新缓冲区

<2>.C++中

要包括头文件 "fstream.h"

/* ofstream ofs("4.txt");

ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));

ofs.close();*/

要注意的是:在读取文件时候ROF表示文件结尾,,readnext会将文件指针指向文件中下一个字符

<3<.MFC中 用CFile类

CFileDialog fileDlg(FALSE);

fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";

fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

fileDlg.m_ofn.lpstrDefExt="txt";

if(IDOK==fileDlg.DoModal())

{

CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);

file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));

file.Close();

}

二进制文件和文本文件,实际上它们都是以二进制数据的方式存储的,文件只是计算机内在中以二进制表示的数据在外部存储介质上的另一种存放形式,对于文本文件来,它只是一种特殊形式的文件,它所存放的第一个字节都可以转换为一个可读的字符

注意:写入和读取文件操作的方式要一致,文本方式写入就用文本方式读取,二进制方式写入就用二进制方式读取.

面试题:给你一个整数,如:98341,将这个整数保存到文件中,要求在以记事本程序打开该文件时,显示是:98341,<要注意字符与整型是通用的,在文件中字符也是以ASCII保存的)

FILE *pFile = fopen(“3.txt”, “w”);

char ch[5];

ch[0] = 9 + 48;

ch[1] = 8 + 48;

ch[2] = 3 + 48;

ch[3] = 4 + 48;

ch[4] = 1 + 48;//0对应的ASCII是48,

fwrite(ch, 1, 5, pFile);//以这种方式写入,在记事本打开时就是98341

fclose(pFile);

4.利用win32 API函数 CreateFile(),及WriteFile()

CreateFile函数将创建或打开下列对象,并返回一个用于读取该对象的句柄:

文件, 管道, 邮槽(在线程通信时会用到), 通信资源, 磁盘设备, 控件台,目录(仅适用于打开操作)

Example:

void CLesson12View::OnFileWrite()

{

// TODO: Add your command handler code here

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

fwrite("C语言文件操作", 1, strlen("C语言文件操作"), pFile);

fclose(pFile);

*/

/*CFile file;

file.Open("2.txt", CFile::modeCreate | CFile::modeWrite);

file.Write("MFC文件操作", strlen("MFC文件操作"));

file.Close();*/

/*CFileDialog m_fileDlg(FALSE);

m_fileDlg.m_ofn.lpstrDefExt = "txt";

m_fileDlg.m_ofn.lpstrTitle ="保存我的文件";

m_fileDlg.m_ofn.lpstrFilter = "Text File(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";

if(IDOK == m_fileDlg.DoModal())

{

CFile mFile;

mFile.Open(m_fileDlg.GetPathName(), CFile::modeCreate | CFile::modeWrite);

mFile.Write("test file write!", strlen("test file write!"));

mFile.Close();

}*/

//定义一个句柄变量

HANDLE hFile;

//创建文件

hFile = CreateFile("5.txt", GENERIC_WRITE, 0,

NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

//接收实际写入的字节数据

DWORD dwWrites;

//写入数据

WriteFile(hFile, "example", strlen("example"), &dwWrites, NULL);

//关闭文件句柄

CloseHandle(hFile);

}

4.注册表读写

1.对win.ini的读写

//::WriteProfileString("http://www.sunxin.org","admin","zhangsan");

/* CString str;

::GetProfileString("http://www.sunxin.org","admin","lisi",

str.GetBuffer(100),100);

AfxMessageBox(str);*/

2.注册表的读写

HKEY hKey;

DWORD dwAge=30;

RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);

RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));

RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);

RegCloseKey(hKey);以上是写入

Example:

void CLesson12View::OnRegWrite()

{

// TODO: Add your command handler code here

//注册表操作

//RegCreateKey()

//RegCloseKey()如果不需要以上找到注册表项

HKEY mKey;

DWORD dAge = 30;

::RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\www.sunxin.org\\admin", &mKey);

::RegSetValue(mKey, NULL, REG_SZ, "zhangsan", strlen("zhangsan"));

::RegSetValueEx(mKey, "age", 0, REG_DWORD, (CONST BYTE*)&dAge, 4);

::RegCloseKey(mKey);//在不使用的时候要调用这个函数

}

void CLesson12View::OnRegRead()

{

// TODO: Add your command handler code here

/*LONG lValue;

RegQueryValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\www.sunxin.org\\admin", NULL, &lValue);

char *ch = new char[lValue];

RegQueryValue(HKEY_LOCAL_MACHINE, "SOFTWARE\\www.sunxin.org\\admin", ch, &lValue);

MessageBox(ch);*/

HKEY hKey;

::RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\www.sunxin.org\\admin", &hKey);

DWORD dwType;

DWORD dwValue;

DWORD dwAge;

::RegQueryValueEx(hKey, "age", 0, &dwType, (LPBYTE)&dwAge, &dwValue);

CString str;

str.Format("Age = %d", dwAge);

MessageBox(str);

}

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