C语言的文件系统是带缓冲的文件系统(fflush)。
注意以二进制文件和文本文件在读取上的区别(记住:对文件的操作写入方式与读取方式要保持一致)。
1、当按照文本方式向文件中写入数据时,一旦遇到“换行”字符(ASCII码为10),则会转换为“回车—换行”(ASCII码分别为13,10)。在读取文件时,一旦遇到回车—换行(ASCII码分别为13,10)的组合,则会转换为换行字符(ASCII码为10)。
2、当按照二进制方式向文件中写入数据时,则会将数据在内存中的存储形式原样输出到文件中。
3、因此, 我们在对一个文件进行操作以前,首先,我们要清楚这个文件到底是文本文件还是二进制文件。文件文件用文本方式打开,二进制文件用二进制方式打开
如果我们要操作一个二进制文件,那么我们就以二进制方式打开(理论上也可以以文件方式打开,但是如果写的二进制数据里面有45时,会转化成45,42存储,见1.这是很有可能发生的)。同时读写的时候用fread,fwrite这两个函数。
如果我要操作一个文本文件,那么我们就以文本的方式打开(理论上也可以以二进制方式打开,但是不保险)。同时读写的时候用读写字符的那些函数fprintf,fscanf ,fgetc,fputc,putw,getw,fgetc,fputs.
4、整数和字符串的转换 itoa
FILE *pFile;
int i=55;
pFile=fopen("1.txt","w");
fwrite("text.txt",1,strlen("text.txt"),pFile);
fflush(pFile);//把缓冲区的数据写入文件
fwrite("text2.txt",1,strlen("text2.txt"),pFile);
fseek(pFile,0,SEEK_SET);
// fwrite("text34.txt",1,strlen("text34.txt"),pFile);
fwrite(&i,4,1,pFile);
fflush(pFile);
fclose(pFile);
实验:用记事本和可用十六进制打开的工具打开1.txt文件,你看到的和你想的一样么?为什么不一样?如果我想用记事本打开文件可以看到我们定义的i=55的55,我们应该怎么做?提示:文件都是以ASCII存储数据的。
FILE *pFile=fopen("1.txt","r");
/*char ch[100];
memset(ch,0,100);
fread(ch,1,100,pFile);*/
char *pStr;
fseek(pFile,0,SEEK_END);
int len=ftell(pFile);
pStr=new char[len+1];
memset(pStr,0,len+1);
rewind(pFile);
fread(pStr,1,len,pFile);
MessageBox(pStr);
C++对文件的操作:
ofstream类和ifstream类
ofstream of("1.txt");
of.write("dddd",strlen("dddd"));
of.close();
win32 API对文件的操作:
HANDLE hFile;
hFile=CreateFile("2.txt",GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD nWritelen;
WriteFile(hFile,"weiyongming",strlen("weiyongming"),&nWritelen,NULL);
CloseHandle(hFile);
HANDLE hFile;
hFile=CreateFile("2.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dwRead;
ReadFile(hFile,ch,100,&dwRead,NULL);
ch[dwRead]=0;
CloseHandle(hFile);
MessageBox(ch);
MFC对文件的操作:
CFile is the base class for Microsoft Foundation file classes. It directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.CFile works in conjunction with the CArchive class to support serialization of Microsoft Foundation Class objects.
CFile file("3.txt",CFile::modeCreate |CFile::modeWrite );
file.Write("weiyongming",strlen("weiyongming"));
file.Close();
CFile file("3.txt",CFile::modeRead);
DWORD dwFileLength;
dwFileLength=file.GetLength();
char *ch=new char[dwFileLength+1];
ch[dwFileLength]=0;
file.Read(ch,dwFileLength);
file.Close();
MessageBox(ch);
CFileDialog的应用(CFontDialog和CColorDialog的使用也类似):
CFileDialog filedlg(FALSE);
filedlg.m_ofn.lpstrDefExt="txt";
filedlg.m_ofn.lpstrTitle="保存文件";
filedlg.m_ofn.lpstrFilter="text files(*.txt)\0*.txt\0all file(*.*)\0*.*\0\0";
if (IDOK==filedlg.DoModal())
{
CFile file(filedlg.GetFileName(),CFile::modeCreate |CFile::modeWrite );
file.Write("DLG",strlen("DLG"));
file.Close();
}
CFileDialog filedlg(TRUE);
filedlg.m_ofn.lpstrTitle="打开文件";
filedlg.m_ofn.lpstrFilter="text files(*.txt)\0*.txt\0all file(*.*)\0*.*\0\0";
if (IDOK==filedlg.DoModal())
{
CFile file(filedlg.GetFileName(),CFile::modeRead);
DWORD dwFileLength;
dwFileLength=file.GetLength();
char *ch=new char[dwFileLength+1];
ch[dwFileLength]=0;
file.Read(ch,dwFileLength);
file.Close();
MessageBox(ch);
}
注册表的操作:
RegCreateKey
RegSetValue
RegCloseKey
RegQueryValue
RegOpenKey
SetRegistryKey