STLA資料讀入操作

2021-07-11 06:07:14 字數 4594 閱讀 5262

**:

在windows平台對檔案進行訪問操作可選的方案有很多,如果採用純c,則需要用到file*等,當然也可以直接呼叫windows api來做;如果採用c++,首先想到的就是檔案流fstream。雖然在com層面上,我們還可以使用istream來實現檔案的讀寫,其效率也非常高。不過本文僅對c++流操作做簡單的**,相比於windows api或istream,c++的流操作通用性更好一些,因為你能輕鬆將**移植到其它平台上。

fstream有兩個派生類,即ifstream和ofstream,分別對應輸入檔案流、輸出檔案流。在使用它們之前,必須將它們的標頭檔案包含到你的cpp檔案中。

建立乙個檔案流的方法很簡單:

[cpp]view plain

copy

print

? ifstream fin;  

fin.open("c:\filename.txt"

);  

這樣就建立了乙個輸入檔案流fin,它對應的檔案是c盤根目錄下的filename.txt。實際上,open方法還包含乙個引數mode,用以指定其開啟方式。

ios::in以讀取方式開啟檔案

ios::out以寫入方式開啟檔案

ios::ate訪問指標在檔案末尾

寫入時採用追加方式

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

? // 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

[cpp]view plain

copy

print

? //#include "stdafx.h"

struct

facet ;  

int_tmain(

intargc, _tchar* argv)  

ifstream in(argv[1]);  

if(!in.is_open())   

//var

vectorsolid;  

string line;  

string word;  

//check format

getline(in, line);  

if(line.find(

"solid"

) != 0)   

while

(getline(in, line))   

getline(in, line); //"endloop"

getline(in, line); //"endfacet"

solid.push_back(f);  

}  }  

in.close();  

//output

intcnt = solid.size();  

printf("read %d facet\n"

, cnt);  

for(

inti = 0; i < cnt; i++)   

}  return

0;  

}  

測試檔案為:

cube_corner.stl

[plain]view plain

copy

print

? solid cube_corner  

facet normal 0.0 -1.0 0.0  

outer loop  

vertex 0.0 0.0 0.0  

vertex 1.0 0.0 0.0  

vertex 0.0 0.0 1.0  

endloop  

endfacet  

facet normal 0.0 0.0 -1.0  

outer loop  

vertex 0.0 0.0 0.0  

vertex 0.0 1.0 0.0  

vertex 1.0 0.0 0.0  

endloop  

endfacet  

facet normal 0.0 0.0 -1.0  

outer loop  

vertex 0.0 0.0 0.0  

vertex 0.0 0.0 1.0  

vertex 0.0 1.0 0.0  

endloop  

endfacet  

facet normal 0.577 0.577 0.577  

outer loop  

vertex 1.0 0.0 0.0  

vertex 0.0 1.0 0.0  

vertex 0.0 0.0 1.0  

endloop  

endfacet  

endsolid  

輸入結果為:

[plain]view plain

copy

print

? read 4 facet  

facet 1:  

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)  

facet 2:  

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)  

facet 3:  

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)  

facet 4:  

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)  

press any key to continue . . .  

python讀入資料

二.從檔案讀取資料 語法格式 var name input 提示資訊 讀取鍵盤輸入,儲存為string型別,並賦值給變數var name 當需要輸入多個資料時 list var list var input 請輸入多個資料,以空格區分 split int list var 0 語法格式 import...

判斷讀入資料檔案結尾 從檔案讀入資料

參考 在使用c c 讀檔案的時候,使用eof 這個函式來判斷檔案是否為空或者是否讀到檔案結尾的時候會有一些特殊情況 先看 include include using namespace std int main e return 0 上述 在vs2012下編譯執行,發現,當檔案結尾沒有空行時,結果正...

R語言讀入資料

一 r 匯入文字檔案 r中匯入資料的基本命令是scan 比較常用的命令有read.table read.csv read.fwf 等,這些命令其實在內部也是通過呼叫scan 實現的,它們可以看作是scan 的簡化。r從使用分界符的文字 delimited text 讀取資料到資料框的命令為read....