STLA数据读入操作

转自:http://blog.csdn.net/ixsea/article/details/11736005


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

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

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

[cpp]  view plain  copy
 print ?
  1. ifstream fin;  
  2. 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

[cpp]  view plain  copy
 print ?
  1. // stdafx.h : include file for standard system include files,  
  2. // or project specific include files that are used frequently, but  
  3. // are changed infrequently  
  4. //  
  5.   
  6. #pragma once  
  7.   
  8. #include "targetver.h"  
  9.   
  10. #include <stdio.h>  
  11. #include <tchar.h>  
  12. //added  
  13. #include <iostream>  
  14. #include <sstream>  
  15. #include <fstream>  
  16. #include <string>  
  17. #include <vector>  
  18. using namespace std;  
  19.   
  20. // TODO: reference additional headers your program requires here  
readstla.cpp

[cpp]  view plain  copy
 print ?
  1. // readstla.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5.   
  6. struct facet {  
  7.     float normal[3];  
  8.     float vertex[3][3];  
  9. };  
  10.   
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.     if (argc < 2) {  
  14.         printf("specify an input file!\n");  
  15.         return 1;  
  16.     }  
  17.     ifstream in(argv[1]);  
  18.     if (!in.is_open()) {  
  19.         printf("fail to open file!\n");  
  20.         return 1;  
  21.     }  
  22.     //var  
  23.     vector<facet> solid;  
  24.     string line;  
  25.     string word;  
  26.     //check format  
  27.     getline(in, line);  
  28.     if (line.find("solid") != 0) {  
  29.         printf("wrong file format!\n");  
  30.         in.close();  
  31.         return 1;  
  32.     }  
  33.     while (getline(in, line)) {  
  34.         if (line.find("facet normal") != string::npos) {  
  35.             facet f;  
  36.             //read normal  
  37.             stringstream ns(line);  
  38.             ns >> word; //eat "facet"  
  39.             ns >> word; //eat "normal"  
  40.             ns >> f.normal[0] >> f.normal[1] >> f.normal[2];  
  41.             //read vertices  
  42.             getline(in, line); //"outer loop"  
  43.             for (int i = 0; i < 3; i++) {  
  44.                 getline(in, line);  
  45.                 stringstream vs(line);  
  46.                 vs >> word; //eat "vertex"  
  47.                 vs >> f.vertex[i][0] >> f.vertex[i][1] >> f.vertex[i][2];  
  48.             }  
  49.             getline(in, line); //"endloop"  
  50.             getline(in, line); //"endfacet"  
  51.             solid.push_back(f);  
  52.         }  
  53.     }  
  54.     in.close();  
  55.     //output  
  56.     int cnt = solid.size();  
  57.     printf("read %d facet\n", cnt);  
  58.     for (int i = 0; i < cnt; i++) {  
  59.         facet& f = solid[i];  
  60.         printf("\nfacet %d:\nnormal = (%f, %f, %f)\n", \  
  61.                        i+1, f.normal[0], f.normal[1], f.normal[2]);  
  62.         for (int j = 0; j < 3; j++) {  
  63.             printf("vertex[%d] = (%f, %f, %f)\n", \  
  64.                               j+1, f.vertex[j][0], f.vertex[j][1], f.vertex[j][2]);  
  65.         }  
  66.     }  
  67.     return 0;  
  68. }  
测试文件为:

cube_corner.stl

[plain]  view plain  copy
 print ?
  1. solid cube_corner  
  2.   facet normal 0.0 -1.0 0.0  
  3.     outer loop  
  4.       vertex 0.0 0.0 0.0  
  5.       vertex 1.0 0.0 0.0  
  6.       vertex 0.0 0.0 1.0  
  7.     endloop  
  8.   endfacet  
  9.   facet normal 0.0 0.0 -1.0  
  10.     outer loop  
  11.       vertex 0.0 0.0 0.0  
  12.       vertex 0.0 1.0 0.0  
  13.       vertex 1.0 0.0 0.0  
  14.     endloop  
  15.   endfacet  
  16.   facet normal 0.0 0.0 -1.0  
  17.     outer loop  
  18.       vertex 0.0 0.0 0.0  
  19.       vertex 0.0 0.0 1.0  
  20.       vertex 0.0 1.0 0.0  
  21.     endloop  
  22.   endfacet  
  23.   facet normal 0.577 0.577 0.577  
  24.     outer loop  
  25.       vertex 1.0 0.0 0.0  
  26.       vertex 0.0 1.0 0.0  
  27.       vertex 0.0 0.0 1.0  
  28.     endloop  
  29.   endfacet  
  30. endsolid  
输入结果为:

[plain]  view plain  copy
 print ?
  1. read 4 facet  
  2.   
  3. facet 1:  
  4. normal = (0.000000, -1.000000, 0.000000)  
  5. vertex[1] = (0.000000, 0.000000, 0.000000)  
  6. vertex[2] = (1.000000, 0.000000, 0.000000)  
  7. vertex[3] = (0.000000, 0.000000, 1.000000)  
  8.   
  9. facet 2:  
  10. normal = (0.000000, 0.000000, -1.000000)  
  11. vertex[1] = (0.000000, 0.000000, 0.000000)  
  12. vertex[2] = (0.000000, 1.000000, 0.000000)  
  13. vertex[3] = (1.000000, 0.000000, 0.000000)  
  14.   
  15. facet 3:  
  16. normal = (0.000000, 0.000000, -1.000000)  
  17. vertex[1] = (0.000000, 0.000000, 0.000000)  
  18. vertex[2] = (0.000000, 0.000000, 1.000000)  
  19. vertex[3] = (0.000000, 1.000000, 0.000000)  
  20.   
  21. facet 4:  
  22. normal = (0.577000, 0.577000, 0.577000)  
  23. vertex[1] = (1.000000, 0.000000, 0.000000)  
  24. vertex[2] = (0.000000, 1.000000, 0.000000)  
  25. vertex[3] = (0.000000, 0.000000, 1.000000)  
  26. Press any key to continue . . .  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值