国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++流操作之fstream用法介紹

2020-01-26 15:51:31
字體:
來源:轉載
供稿:網友

在Windows平臺對文件進行存取操作可選的方案有很多,如果采用純C,則需要用到File*等,當然也可以直接調用Windows API來做;如果采用C++,首先想到的就是文件流fstream。雖然在COM層面上,我們還可以使用IStream來實現文件的讀寫,其效率也非常高。不過本文僅對C++流操作做簡單的探討,相比于Windows API或IStream,C++的流操作通用性更好一些,因為你能輕松將代碼移植到其它平臺上。

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 <stdio.h> 
#include <tchar.h> 
//added 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <string> 
#include <vector> 
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[3]; 
    float vertex[3][3]; 
}; 

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

    if (argc < 2) { 
        printf("specify an input file!/n"); 
        return 1; 
    } 
    ifstream in(argv[1]); 
    if (!in.is_open()) { 
        printf("fail to open file!/n"); 
        return 1; 
    } 
    //var 
    vector<facet> solid; 
    string line; 
    string word; 
    //check format 
    getline(in, line); 
    if (line.find("solid") != 0) { 
        printf("wrong file format!/n"); 
        in.close(); 
        return 1; 
    } 
    while (getline(in, line)) { 
        if (line.find("facet normal") != string::npos) { 
            facet f; 
            //read normal 
            stringstream ns(line); 
            ns >> word; //eat "facet" 
            ns >> word; //eat "normal" 
            ns >> f.normal[0] >> f.normal[1] >> f.normal[2]; 
            //read vertices 
            getline(in, line); //"outer loop" 
            for (int i = 0; i < 3; i++) { 
                getline(in, line); 
                stringstream vs(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 
    int cnt = solid.size(); 
    printf("read %d facet/n", cnt); 
    for (int i = 0; i < cnt; 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 (int j = 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]); 
        } 
    } 
    return 0; 
}

測試文件為:
cube_corner.stl
復制代碼 代碼如下:


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 

輸入結果為:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宣恩县| 舟曲县| 正阳县| 文登市| 正安县| 榆树市| 项城市| 北辰区| 区。| 郯城县| 那曲县| 望城县| 芦山县| 江城| 徐州市| 宁阳县| 庆安县| 金寨县| 巨野县| 鱼台县| 南岸区| 项城市| 琼中| 商水县| 通州区| 桃源县| 县级市| 兰溪市| 仁怀市| 高淳县| 石嘴山市| 山东省| 巫山县| 札达县| 沁阳市| 新巴尔虎右旗| 香河县| 镇原县| 徐闻县| 平武县| 连城县|