VC++ chap12 file
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); //将整数转化为字符。
VC++ chap12 file的更多相关文章
- VC 2008 Express下安装OpenCV2.3.1
VC 2008 Express下安装OpenCV2.3.1 注意: 下列文档以VC2008 Express为例,VC2010下的配置应与本文档类似. VC 6.0不被OpenCV 2.3.1支持. ...
- Rational Rose 2003 逆向工程转换C++ / VC++ 6.0源代码成UML类图
目录 1.安装&破解Rational Rose 2003 1.1 安装Rose 2003 1.2 破解Rose 2003 1.3运行出错“没有找到suite objects.dl” 2. Ra ...
- 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】
文章来自于:http://blog.renren.com/share/246648717/8171467499 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴 ...
- VC6使用技巧
1.检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键“Ctrl+]”.如果括号匹配正确,光标就跳到匹配的括号处,否则 ...
- vs2005配置OpenCv2.3.1
编译OpenCv 1 用CMake导出VC++项目文件 运行cmake-gui,设置where is the source code路径为OpenCV安装路径(本文档假定安装位置为:c:\OpenCV ...
- Rational Rose 2003 逆向工程转换C++源代码成UML类图
主要介绍用户如何使用Rose的逆向工程生成UML模型,并用来进行C++代码的结构分析. Rational Rose可以支持标准C++和Visual C++的模型到代码的转换以及逆向工程.下面将详细地说 ...
- VC6.0实用小技巧
VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...
- vc6.0的一些快捷键
1.检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键“Ctrl+]”.如果括号匹配正确,光标就跳到匹配的括号处 ...
- Create a Visual C++ Wizard for Visual Studio 2005
from:http://www.codeguru.com/cpp/v-s/devstudio_macros/customappwizards/article.php/c12775/Create-a-V ...
随机推荐
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- [读书笔记]OSGI-灵活的类加载器架构
以下内容来自周志明的<深入理解Java虚拟机>. 学习JEE规范,去看JBoss源码:学习类加载器,就去看OSGI源码. OSGI,即Open Service Gateway Initia ...
- css中选择器的使用
css是英文Cascading Style Sheets的缩写.它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.我们再将html比喻 ...
- Android Listview 性能优化
首先我一般使用的适配器是BaseAdapter,其中有两个方法最主要,分别是: getCount,getView, 在对Listview 进行优化的时候,首先使用 convertview 和viewH ...
- Python UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
#!/usr/bin/python# -*- coding: utf-8 -*- 解决方法: 可以看到我的版本是2.6的,所以打开/usr/lib64/python2.6/site.py 红框里本来是 ...
- 使Maven 2在package、install等阶段跳过运行Test的配置
方法1: To skip running the tests for a particular project, set the skipTests property to true.<proj ...
- CompositeConfiguration的用法
public class Mytest { private static ApplicationContext applicationContext; public static void main( ...
- node版本管理器nvm(服务器项目相关)
git项目 https://github.com/creationix/nvm 1.下载并安装NVM脚本 curl https://raw.githubusercontent.com/creation ...
- Fatal error: Call to undefined function imagettftext()解决办法
Fatal error: Call to undefined function imagettftext()解决办法 我的问题是php编译安装时指定了gd的目录,其实不用指定.就可以了 博客分类: ...
- JTAG和SWD连接关系图
经实际测试 SWD最少接线方法: 1.VTref与Vsupply短接 2.JLINK的SWDIO与目标板SWDIO相连 3.JLINK的SWCLK与目标板SWCLK相连 4.JLINK任意一个GND与 ...