VC中怎么读取.txt文件
2016-01-01 16:19
309 查看
一、
CStdioFile
二、
FILE* f = fopen("file name", "mode");
char buff[size];
fread(buff, size, 1, f);
fclose(f);
三、
//用MFC读文件
CFile file("yourfile.txt",CFile::modeRead);
char *pBuf;
int iLen=file.GetLength();
pBuf =new char[iLen 1];
file.Read(pBuf,iLen);
pBuf[iLen]=0;
file.Close();
MessageBox(pBuf);
四、
//用C SDK 读文件
HANDLE hFile;
hFile=CreateFile("2.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dwReads;
ReadFile(hFile,ch,100,&dwReads,NULL);
CloseHandle(hFile);
ch[dwReads]=0;
MessageBox(ch);*
五、
用C读文件
FILE *pFile=fopen("1.txt","rb");
char *pBuf;
fseek(pFile,0,SEEK_END);//移动文件指针到文件末尾
int len=ftell(pFile);//获取当前文件指针在文件中的偏移量,Gets the current position of a file pointer.offset
pBuf=new char[len];
rewind(pFile);//将指针移动到文件头,Repositions the file pointer to the beginning of a file
//也可以用fseek(pFile,0,SEEK_SET);
fread(pBuf,1,len,pFile);
pBuf[len]=0;
fclose(pFile);
MessageBox(pBuf);
CStdioFile
二、
FILE* f = fopen("file name", "mode");
char buff[size];
fread(buff, size, 1, f);
fclose(f);
三、
//用MFC读文件
CFile file("yourfile.txt",CFile::modeRead);
char *pBuf;
int iLen=file.GetLength();
pBuf =new char[iLen 1];
file.Read(pBuf,iLen);
pBuf[iLen]=0;
file.Close();
MessageBox(pBuf);
四、
//用C SDK 读文件
HANDLE hFile;
hFile=CreateFile("2.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
char ch[100];
DWORD dwReads;
ReadFile(hFile,ch,100,&dwReads,NULL);
CloseHandle(hFile);
ch[dwReads]=0;
MessageBox(ch);*
五、
用C读文件
FILE *pFile=fopen("1.txt","rb");
char *pBuf;
fseek(pFile,0,SEEK_END);//移动文件指针到文件末尾
int len=ftell(pFile);//获取当前文件指针在文件中的偏移量,Gets the current position of a file pointer.offset
pBuf=new char[len];
rewind(pFile);//将指针移动到文件头,Repositions the file pointer to the beginning of a file
//也可以用fseek(pFile,0,SEEK_SET);
fread(pBuf,1,len,pFile);
pBuf[len]=0;
fclose(pFile);
MessageBox(pBuf);
相关文章推荐
- 成员方法内存分析
- 什么是域名解析?
- java调用不同package的内部类
- 自定义string类
- SQL Server Convert()函数
- 域名301跳转
- A Curious Matt
- MySQL GTID模式 主从复制跳过错误事务
- 转入中国国家顶级域名需要哪些手续?
- LINUX下编译c++11的代码
- Jeecg社区wiki在开放,最终能够在线看文档啦!!!
- 一个命令禁止恢复Win10硬盘已删文件的技巧
- Linux终端切换和ls、cd、date、shutdown相关命令
- Atmel 官方网站中文版
- leetcode之House Robber
- 中国企业可以注册的域名有哪些?
- n*n(n<20,为正奇数)阶方阵(题型二)
- Android5.1中联系人新建和保存机制
- Context中有一个startActivity方法
- NSURLConnection — POST请求