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的更多相关文章

  1. VC 2008 Express下安装OpenCV2.3.1

    VC 2008 Express下安装OpenCV2.3.1   注意: 下列文档以VC2008 Express为例,VC2010下的配置应与本文档类似. VC 6.0不被OpenCV 2.3.1支持. ...

  2. 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 ...

  3. 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】

    文章来自于:http://blog.renren.com/share/246648717/8171467499 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴 ...

  4. VC6使用技巧

    1.检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键“Ctrl+]”.如果括号匹配正确,光标就跳到匹配的括号处,否则 ...

  5. vs2005配置OpenCv2.3.1

    编译OpenCv 1 用CMake导出VC++项目文件 运行cmake-gui,设置where is the source code路径为OpenCV安装路径(本文档假定安装位置为:c:\OpenCV ...

  6. Rational Rose 2003 逆向工程转换C++源代码成UML类图

    主要介绍用户如何使用Rose的逆向工程生成UML模型,并用来进行C++代码的结构分析. Rational Rose可以支持标准C++和Visual C++的模型到代码的转换以及逆向工程.下面将详细地说 ...

  7. VC6.0实用小技巧

    VC6.0的若干实用小技巧 .检测程序中的括号是否匹配 把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键 “Ctrl+]”.如果括号匹配正确, ...

  8. vc6.0的一些快捷键

    1.检测程序中的括号是否匹配    把光标移动到需要检测的括号(如大括号{}.方括号[].圆括号()和尖括号<>)前面,键入快捷键“Ctrl+]”.如果括号匹配正确,光标就跳到匹配的括号处 ...

  9. 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 ...

随机推荐

  1. 相同vlan之间的相互访问

  2. pyhton 27 pip命令无法使用 没有Scripts文件夹 的解决方法

    1 安装了setuptools http://jingyan.baidu.com/article/fb48e8be52f3166e622e1400.html 2 用ez_setup.py安装了setu ...

  3. Python学习笔记——Day3

    Python字典(Dictionary) 字典是一种可变容器模型,可存储任意类型对象. 字典的每个键值(key => value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花 ...

  4. 如何解决虚拟机克隆导致"Bringing up interface eth0: Error: No suitable device found: no device found for connection 'System eth0'."

    在VMware的虚拟机中克隆CentOS,在重启网卡的时候报错: Bringing up interface eth0:  Error: No suitable device found: no de ...

  5. nova boot instance call flow

    参考http://www.cnblogs.com/popsuper1982/p/3927390.html

  6. 【SVN】自动备份SVN仓库

    仓库的位置为:C:\xxx\SVNRepo\ MyCommonUtils MyStudyProject SVN仓库备份.bat '参考连接:http://www.uml.org.cn/pzgl/201 ...

  7. 使用 Windows AIK 创建自定的客户端系统WIM文件

    Windows 7/2008 的AIK 3.0下载页面:地址链接 1.8G [3.1补充包为1.4G] 安装3.0后,升级为3.1方法: xcopy E:\ "C:\Program File ...

  8. 递归绑定将数据表中的数据按层级更新到 TreeView节点上

     private void bindTreeView1()         {             string sql = "select * from dm_category&quo ...

  9. [家里蹲大学数学杂志]第237期Euler公式的美

    1 Euler 公式 $e^{i\pi}+1=0$ (1) 它把 a.  $e:$ 自然对数的底 $\approx 2. 718281828459$ (数分) b.  $i$: 虚数单位 $=\sqr ...

  10. windows下python3.4安装scikit-learn

    python3.4.0_64位下安装numpy-1.11.1 安装步骤: ​1.在终端CMD中输入: python -m pip install -U pip​​ 2.找到 下载的 numpy-1.1 ...