亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2013年09月17日 15:44:33   作者:  
這篇文章詳細(xì)介紹了C++流操作之fstream的用法,有需要的朋友可以參考一下

在Windows平臺(tái)對(duì)文件進(jìn)行存取操作可選的方案有很多,如果采用純C,則需要用到File*等,當(dāng)然也可以直接調(diào)用Windows API來做;如果采用C++,首先想到的就是文件流fstream。雖然在COM層面上,我們還可以使用IStream來實(shí)現(xiàn)文件的讀寫,其效率也非常高。不過本文僅對(duì)C++流操作做簡(jiǎn)單的探討,相比于Windows API或IStream,C++的流操作通用性更好一些,因?yàn)槟隳茌p松將代碼移植到其它平臺(tái)上。

fstream有兩個(gè)派生類,即ifstream和ofstream,分別對(duì)應(yīng)輸入文件流、輸出文件流。在使用它們之前,必須將它們的頭文件包含到你的cpp文件中。

創(chuàng)建一個(gè)文件流的方法很簡(jiǎn)單:

ifstream fin; 
fin.open("C:\filename.txt"); 
這樣就創(chuàng)建了一個(gè)輸入文件流fin,它對(duì)應(yīng)的文件是C盤根目錄下的filename.txt。實(shí)際上,open方法還包含一個(gè)參數(shù)mode,用以指定其打開方式。
ios::in 以讀取方式打開文件
ios::out 以寫入方式打開文件
ios::ate 存取指針在文件末尾
ios::app 寫入時(shí)采用追加方式
ios::trunc 寫入時(shí)抹去舊數(shù)據(jù)
ios::binary 以二進(jìn)制方式存取
上面的代碼并未指定任何打開方式,則采用默認(rèn)參數(shù):輸入文件流即ios::in,輸出文件流即ios::out。一般在需要組合特殊的mode才顯式指定,比如:
ios::in | ios::binary; //以二進(jìn)制方式讀取文件

除此之外,還可以在構(gòu)造時(shí)指定相應(yīng)的文件路徑和名稱,讓創(chuàng)建過程一步到位。上述代碼可改寫為:

ifstream fin("C:\filename.txt");
與open方法相反的是close方法,它的作用與open正好相反。open是將文件流對(duì)象與外設(shè)中的文件關(guān)聯(lián)起來,close則是解除二者的關(guān)聯(lián)。但是需要注意的是,close還起到清空緩存的作用。最好讓open方法與close方法成對(duì)出現(xiàn)。

創(chuàng)建并打開一個(gè)文件流后,就能像操作標(biāo)準(zhǔn)I/O那樣使用流插入操作符(<<)與流提取操作符(>>)。對(duì)于輸入文件流來說,可以調(diào)用getline函數(shù)從文件流中讀取一整行數(shù)據(jù),這樣就可以讀入含有空格的字符串。

下面是一個(gè)例子,該例的作用是讀取一個(gè)STLA格式的文件。STL是一種常用快速成像文件格式,其格式非常簡(jiǎn)單,特別是ASCII版本(即STLA)。代碼如下所示:

stdafx.h

復(fù)制代碼 代碼如下:

// 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; 
}

測(cè)試文件為:
cube_corner.stl
復(fù)制代碼 代碼如下:


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 

輸入結(jié)果為:

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

相關(guān)文章

  • 一起來了解一下C++中的指針

    一起來了解一下C++中的指針

    這篇文章主要為大家詳細(xì)介紹了C++的指針,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • C++中LibCurl庫使用流程及配置詳解

    C++中LibCurl庫使用流程及配置詳解

    libcurl是一個(gè)跨平臺(tái)的開源網(wǎng)絡(luò)傳輸庫,它支持許多協(xié)議,包括HTTP、HTTPS、FTP、FTPS以及許多其他協(xié)議和文件傳輸方式,本文給大家詳細(xì)介紹了C++中LibCurl庫使用流程及配置,需要的朋友可以參考下
    2024-02-02
  • C/C++語言中全局變量重復(fù)定義問題的解決方法

    C/C++語言中全局變量重復(fù)定義問題的解決方法

    這篇文章主要給大家介紹了關(guān)于C/C++語言中全局變量重復(fù)定義問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • C++實(shí)踐Time類中的運(yùn)算符重載參考方法

    C++實(shí)踐Time類中的運(yùn)算符重載參考方法

    今天小編就為大家分享一篇關(guān)于C++實(shí)踐Time類中的運(yùn)算符重載參考方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • 詳解C語言中的預(yù)處理命令

    詳解C語言中的預(yù)處理命令

    初學(xué)C語言的時(shí)候,我們會(huì)在開頭寫下一句話,#include<stdio.h>,這就是預(yù)處理命令,下面我們通過這篇文章來了解一下,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12
  • C++中二進(jìn)制數(shù)據(jù)序列化和反序列化詳解

    C++中二進(jìn)制數(shù)據(jù)序列化和反序列化詳解

    這篇文章主要為大家詳細(xì)介紹了C++中二進(jìn)制數(shù)據(jù)序列化和反序列化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下
    2023-11-11
  • C經(jīng)典算法之二分查找法

    C經(jīng)典算法之二分查找法

    這篇文章主要介紹了C經(jīng)典算法之二分查找法的相關(guān)資料,這里提供兩種方法幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-10-10
  • C++初階學(xué)習(xí)之模板進(jìn)階

    C++初階學(xué)習(xí)之模板進(jìn)階

    這篇文章主要為大家介紹了C++模板進(jìn)階,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C語言程序環(huán)境編譯+鏈接理論

    C語言程序環(huán)境編譯+鏈接理論

    這篇文章主要介紹了C語言程序環(huán)境編譯+鏈接理論,下面文章基于C語言的相關(guān)資料展開對(duì)編譯和鏈接的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • C語言動(dòng)態(tài)內(nèi)存泄露常見問題內(nèi)存分配改進(jìn)方法詳解

    C語言動(dòng)態(tài)內(nèi)存泄露常見問題內(nèi)存分配改進(jìn)方法詳解

    今天遇見了一道有意思的內(nèi)存泄露題目,特地分享給大家,相信屏幕前的你學(xué)習(xí)完一定有所收獲,預(yù)祝讀者學(xué)習(xí)愉快,多多進(jìn)步早日升職加薪
    2021-10-10

最新評(píng)論