首页 技术 正文
技术 2022年11月8日
0 收藏 949 点赞 1,564 浏览 3082 个字

file operation

_______C语言对文件操作的支持

fopen accepts paths that are valid on the file system at the point of execution;

____write

FILE *pFile = fopen(“1,txt”,”w”);    //firstly, file should be opened
fwrite(“a carrot”,1,strlen(“a carrot”),pFile);
fseek(pFile,10,SEEK_END);       //set the next write position
fwrite(“welcome angle”,1,strlen(“welcome angle”),pFile);
fflush(pFile);              //write from buffer to file, because C apply file buffer system, data will first be stored in buffer until the buffer is full,if we want to write data into file immediately fflush is called. or fclose function to close the file.

____read

FILE *pFile = fopen("1.txt","r");
char *pBuf;
fseek(pFile,0,SEEK_END);    //move to end of file   
int len = ftell(pFile);     //get the len of file
pBuf = new char[len + 1];   //need '\0' for ending
fread(pBuf,1,len,pFile);
pBuf[len] = 0;
fclose(pFile);
MessageBox(pBuf);

_

AT: 1),读取文件数据时,如果是字符数据,在定义用来保存该数据的字符数组时,需多分配一个字节,用来存放表示字符串结尾的字符: ‘\0’. 或者将数组先清零  memset(ch, 0,100);

2),读取文件内容时,应正确地设置文件指针的位置。

fseek(pFile,0,SEEK_SET);

rewind(pFile); 将文件指针重新放置到文件开始处。

_________C++读文件

添加头文件 #include <fstream.h>

ifstream ifs(“4.txt”);

char ch[100];

memset(ch,0,100);

ifs.read(ch,100);

ifs.close();

MessageBox(ch);

C++写文件

ofstream  ofs(“2.txt”);

ofs.write(“sunxin”,strlen(“sunxin”));

ofs.close();

3,W32 API对文件的操作

读文件

HANDLE hFile;

hFile=CreateFile(“5.txt”,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

char ch[100];

DWORD dwReads;

ReadFile(hFile,ch,100,&dwReads,NULL);

ch[dwReads]=0;

CloseHandle(hFile);

MessageBox(ch);

写文件

HANDLE hFile;

hFile = CreateFile(“5.txt”,GENERIC_WRITE,0,NULL,CREATE_NEW,

FILE_ATTRIBUTE_NORMAL,NULL);

DWORD dwWrites;

WriteFile(hFile,”sunxinweb”,strlen(“sunxinweb”),&dwWrites,NULL);

CloseHandle(hFile);

_________ MFC 对文件的读取和写入

读文件

CFile file(“6.txt”,CFile::modeRead);

char *pBuf;

DWORD dwFileLen;

dwFileLen = file.GetLength();

pBuf=new char[dwFileLen+1];

pBuf[dwFileLen]=0;

file.Read(pBuf,dwFileLen);

file.Close();

MessageBox(pBuf);

写文件

CFile file(“6.txt”,CFile::modeCreate|CFile::modeWrite);

file.Write(“sunxin”,strlen(“sunxin”));

file.Close();

_______打开文件对话框,另存为对话框   CFileDialog

CFileDialog    fileDlg(TRUE);

fileDlg.m_ofn.lpstrTitle=”my file open dialog”;

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::modeRead);

char *pBuf;

DWORD dwFileLen;

dwFileLen = file.GetLength();

pBuf=new char[dwFileLen+1];

pBuf[dwFileLen]=0;

file.Read(pBuf,dwFileLen);

file.Close();

MessageBox(pBuf);

}

Write:

CFileDialog fileDlg(FALSE);              //build save as dialog

fileDlg.m_ofn.lpstrTitle=”my file save dialog”;

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(“hello rabbit”,strlen(“hello rabbit”));

file.Close();

}

Note: CFileDialog 提供两个函数: GetFileName,GetFilePath

12.6 win.ini文件的访问

___________二进制文件和文本文件

C中默认按照文本方式打开文件的。

文件:计算机内存中以二进制表示的数据在外部存储介质上的另一种存放形式。

分为文本文件和二进制文件。

当按照文本方式向文件中写入数据时,一旦遇到“换行”(ascll码为10)会转换为“回车-换行”(ascll码分别为13,10),读的时候,则反过来。

所以写入和读取时格式需统一。

文本文件存放的每一个字节都可以转换为一个可读的字符。如打开一个文本格式文件,文件中存储的每一个字节的数据都要作为ascll码转换为相应的字符,如果它的某一个字节的数据转换为字符后是不可读的,就会显示乱码。

itoa(98341, ch,10); //将整数转化为字符。


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,564
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905