设为首页 加入收藏

TOP

C++流操作之fstream(一)
2013-09-26 18:39:05 来源: 作者: 【 】 浏览:86
Tags:操作 fstream

  在Windows平台对文件进行存取操作可选的方案有很多,如果采用纯C,则需要用到File*等,当然也可以直接调用Windows API来做;如果采用C++(www.cppentry.com),首先想到的就是文件流fstream。虽然在COM层面上,我们还可以使用IStream来实现文件的读写,其效率也非常高。不过本文仅对C++(www.cppentry.com)流操作做简单的探讨,相比于Windows API或IStream,C++(www.cppentry.com)的流操作通用性更好一些,因为你能轻松将代码移植到其它平台上。

  fstream有两个派生类,即ifstream和ofstream,分别对应输入文件流、输出文件流。在使用它们之前,必须将它们的头文件包含到你的cpp文件中。

  创建一个文件流的方法很简单:

  ifstream fin;

  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; //以二进制方式读取文件

  除此之外,还可以在构造时指定相应的文件路径和名称,让创建过程一步到位。上述代码可改写为:

  ifstream fin("C:\filename.txt");

  与open方法相反的是close方法,它的作用与open正好相反。open是将文件流对象与外设中的文件关联起来,close则是解除二者的关联。但是需要注意的是,close还起到清空缓存的作用。最好让open方法与close方法成对出现。

  创建并打开一个文件流后,就能像操作标准I/O那样使用流插入操作符(<<)与流提取操作符(>>)。对于输入文件流来说,可以调用getline函数从文件流中读取一整行数据,这样就可以读入含有空格的字符串。

  下面是一个例子,该例的作用是读取一个STLA格式的文件。STL是一种常用快速成像文件格式,其格式非常简单,特别是ASCII版本(即STLA)。代码如下所示:

  stdafx.h

  // stdafx.h : include file for standard system include files,

  // or project specific include files that are used frequently, but

  // are changed infrequently

  //

  #pragma once

  #include "targetver.h"

  #include

  #include

  //added

  #include

  #include

  #include

  #include

  #include

  using namespace std;

  // TODO: reference additional headers your program requires here

  readstla.cpp

  // readstla.cpp : Defines the entry point for the console application.

  //

  #include "stdafx.h"

  struct facet {

  float normal ;

  float vertex ;

  };

  int _tmain(int argc, _TCHAR* argv[])

  {

  if (argc < 2) {

  printf("specify an input file!\n");

  return 1;

  }

  ifstream in(argv );

  if (!in.is_open()) {

  printf("fail to open file!\n");

  return 1;

  }

  //var

  vector solid;

  string line;

  string word;

  //check format

  getline(in, line);

  if (line.find("solid") != 0) {

  printf("wrong file format!\n");

  in.close();

  return 1;

  }

   

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇用VC++编制FTP客户端程序 下一篇c++中的messagebox()用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: