在Windows平台对文件进行存取操作可选的方案有很多,如果采用纯C,则需要用到File*等,当然也可以直接调用WindowsAPI来做;如果采用C++,首先想到的就是文件流fstream。虽然在COM层面上,我们还可以使用IStream来实现文件的读写,其效率也非常高。不过本文仅对C++流操作做简单的探讨,相比于WindowsAPI或IStream,C++的流操作通用性更好一些,因为你能轻松将代码移植到其它平台上。
fstream有两个派生类,即ifstream和ofstream,分别对应输入文件流、输出文件流。在使用它们之前,必须将它们的头文件包含到你的cpp文件中。
创建一个文件流的方法很简单:
ifstreamfin;
fin.open("C:\filename.txt");
这样就创建了一个输入文件流fin,它对应的文件是C盘根目录下的filename.txt。实际上,open方法还包含一个参数mode,用以指定其打开方式。
ios::in以读取方式打开文件
ios::out以写入方式打开文件
ios::ate存取指针在文件末尾
ios::app写入时采用追加方式
ios::trunc写入时抹去旧数据
ios::binary以二进制方式存取
上面的代码并未指定任何打开方式,则采用默认参数:输入文件流即ios::in,输出文件流即ios::out。一般在需要组合特殊的mode才显式指定,比如:
ios::in|ios::binary;//以二进制方式读取文件
除此之外,还可以在构造时指定相应的文件路径和名称,让创建过程一步到位。上述代码可改写为:
ifstreamfin("C:\filename.txt");
与open方法相反的是close方法,它的作用与open正好相反。open是将文件流对象与外设中的文件关联起来,close则是解除二者的关联。但是需要注意的是,close还起到清空缓存的作用。最好让open方法与close方法成对出现。
创建并打开一个文件流后,就能像操作标准I/O那样使用流插入操作符(<>)。对于输入文件流来说,可以调用getline函数从文件流中读取一整行数据,这样就可以读入含有空格的字符串。
下面是一个例子,该例的作用是读取一个STLA格式的文件。STL是一种常用快速成像文件格式,其格式非常简单,特别是ASCII版本(即STLA)。代码如下所示:
stdafx.h
代码如下:
//stdafx.h:includefileforstandardsystemincludefiles,
//orprojectspecificincludefilesthatareusedfrequently,but
//arechangedinfrequently
//
#pragmaonce
#include"targetver.h"
#include
#include
//added
#include
#include
#include
#include
#include
usingnamespacestd;
//TODO:referenceadditionalheadersyourprogramrequireshere
readstla.cpp
//readstla.cpp:Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
structfacet{
floatnormal[3];
floatvertex[3][3];
};
int_tmain(intargc,_TCHAR*argv[])
{
if(argc<2){
printf("specifyaninputfile!\n");
return1;
}
ifstreamin(argv[1]);
if(!in.is_open()){
printf("failtoopenfile!\n");
return1;
}
//var
vectorsolid;
stringline;
stringword;
//checkformat
getline(in,line);&nbs
p;
if(line.find("solid")!=0){
printf("wrongfileformat!\n");
in.close();
return1;
}
while(getline(in,line)){
if(line.find("facetnormal")!=string::npos){
facetf;
//readnormal
stringstreamns(line);
ns>>word;//eat"facet"
ns>>word;//eat"normal"
ns>>f.normal[0]>>f.normal[1]>>f.normal[2];
//readvertices
getline(in,line);//"outerloop"
for(inti=0;i<3;i++){
getline(in,line);
stringstreamvs(line);
vs>>word;//eat"vertex"
vs>>f.vertex[i][0]>>f.vertex[i][1]>>f.vertex[i][2];
}
getline(in,line);//"endloop"
getline(in,line);//"endfacet"
solid.push_back(f);
}
}
in.close();
//output
intcnt=solid.size();
printf("read%dfacet\n",cnt);
for(inti=0;i
facet&f=solid[i];
printf("\nfacet%d:\nnormal=(%f,%f,%f)\n",\
i+1,f.normal[0],f.normal[1],f.normal[2]);
for(intj=0;j<3;j++){
printf("vertex[%d]=(%f,%f,%f)\n",\
j+1,f.vertex[j][0],f.vertex[j][1],f.vertex[j][2]);
}
}
return0;
}
测试文件为:
cube_corner.stl
代码如下:
solidcube_corner
facetnormal0.0-1.00.0
outerloop
vertex0.00.00.0
vertex1.00.00.0
vertex0.00.01.0
endloop
endfacet
facetnormal0.00.0-1.0
outerloop
vertex0.00.00.0
vertex0.01.00.0
 
; vertex1.00.00.0
endloop
endfacet
facetnormal0.00.0-1.0
outerloop
vertex0.00.00.0
vertex0.00.01.0
vertex0.01.00.0
endloop
endfacet
facetnormal0.5770.5770.577
outerloop
vertex1.00.00.0
vertex0.01.00.0
vertex0.00.01.0
endloop
endfacet
endsolid
输入结果为:
read4facet
facet1:
normal=(0.000000,-1.000000,0.000000)
vertex[1]=(0.000000,0.000000,0.000000)
vertex[2]=(1.000000,0.000000,0.000000)
vertex[3]=(0.000000,0.000000,1.000000)
facet2:
normal=(0.000000,0.000000,-1.000000)
vertex[1]=(0.000000,0.000000,0.000000)
vertex[2]=(0.000000,1.000000,0.000000)
vertex[3]=(1.000000,0.000000,0.000000)
facet3:
normal=(0.000000,0.000000,-1.000000)
vertex[1]=(0.000000,0.000000,0.000000)
vertex[2]=(0.000000,0.000000,1.000000)
vertex[3]=(0.000000,1.000000,0.000000)
facet4:
normal=(0.577000,0.577000,0.577000)
vertex[1]=(1.000000,0.000000,0.000000)
vertex[2]=(0.000000,1.000000,0.000000)
vertex[3]=(0.000000,0.000000,1.000000)
Pressanykeytocontinue...
您可能感兴趣的文章:C++ofstream与ifstream详细用法C++中关于[]静态数组和new分配的动态数组的区别分析C++对数组的引用实例分析C/C++中获取数组长度的方法示例C++普通函数指针与成员函数指针实例解析C++动态数组类的封装实例C++中new与delete、malloc与free应用分析C++命名空间实例解析C++模板特例化应用实例C++中fstream,ifstream及ofstream用法浅析