太久没写文章了,今天水一篇

前几天写c++读取一个二进制数据文件出了个小问题,发现读不出来,代码大概是如下这样子的

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    fstream fs("data",fstream::binary);
    char data[3];
    fs.read(data,3);
    for(int i=0;i<3;++i){
        cout<<(int)data[i]<<" ";
    }
    cout<<endl;
    return 0;
}

后来发现把fstream换成ifstream就可以了

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ifstream fs("data",fstream::binary);
    char data[3];
    fs.read(data,3);
    for(int i=0;i<3;++i){
        cout<<(int)data[i]<<" ";
    }
    cout<<endl;
    return 0;
}

或者如果继续用fstream,构造函数中需要显式的传递个in标志位:

#include <iostream>
#include <fstream>

using namespace std;

int main(){
    fstream fs("data",fstream::binary|fstream::in);
    char data[3];
    fs.read(data,3);
    for(int i=0;i<3;++i){
        cout<<(int)data[i]<<" ";
    }
    cout<<endl;
    return 0;
}

在http://www.cplusplus.com/reference/fstream/ifstream/ifstream/中也有说:

in is always set for ifstream objects (even if explicitly not set in argument mode).

主要是我很少用c++中的stream(除了cout),所以对这方面的api不熟悉。在此记录下来备忘。

4 对 “c++中ifstream和fstream的一个区别”的想法;

  1. Cutte eeen slutsYoour aass iss minje spankingBuck
    naked annd fuckArre youu a boob manPuerto rico giirls
    assVitgin mobilee topp upp caard numbersGayy fuciing macfhines molvie thumbsPrivate beaches byy boaat fll
    nudistJapansse ooil sexMy dqughters beautiful titsPowerebosom having seex outsideAllure amateur cockFedom permssion tto cumNudee kamillia 18Feee dwardf sexx tubeWhat’s happwns whhen you swallow spermRaww tube xxxx pronPebis amputstions
    imagesChange bwck transgenderTeeen giurl sduces marineVirtual sci fii sexWomen in bondage fuckedRhodd gilberrt pornLucdky pornstarTeeen personalized giftsUltijate lesbian videosHanks
    nue galleryHomemade xxx videos caceros de pueblos de yucatan ooob and maineAsiazn prid pictureJaynma oso bbig wwet assesTilaa tequila nakesChap mobilke seex chatBreast
    cancerr awareness promotionFrree nhde p o v videoGarrway nakedBeawch bimini raceDeep throat shyla stylezMomy oches myy cockSexxy pictures oof annn coulterMomny daughteer nudeBeacxh bkkini thong
    pics

  2. Malayszia bikioni girlWhaat cwuses aan adult too urninte during sleepDiick vann dyke dieSeex education theatreNutritio penisGirls squidting pissingGirls wikth tigght roun assVideo showing vibratorsTeeen nasked eveeKentt horn shemaleKollam sexTechniques sexualWomn oof warcraft
    boobsEscort reed roseErotkc misadentures off
    invisible downloadLindey ahyren mmr kin nude

    Feell free tto vsit myy bloog post close up cunnilingus instructions

  3. Do youu mihd iff I quote a feww off your articles as log aas
    I provfide credikt and sourxes ack tto yyour weblog? My bpog siute iis in thhe exact ame nihe
    as yours annd mmy visiitors wuld really benefit fro a lot off tthe informatiokn yoou provkde here.
    Pleease llet mme kjow iff this ook with you. Thanks! ofvd9wuapt13k95s7y9z

  4. We aare a group off volunteers andd opening a neww schheme inn ourr community.
    Yoour websaite provided uus wirh valuaable infco too work on. You’ve
    don a formidrable joob andd ourr enrire coommunity wilol bee tjankful tto you.

    Heree iss myy site – desixxx.biz

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注