1. 程式人生 > >C++ 檔案IO操作 讀寫

C++ 檔案IO操作 讀寫

#include <iostream>  
#include <iomanip>
#include <fstream>  
#include <stdlib.h>
#include <vector>
using namespace std;  

typedef struct ImageList
{
    double timeStamp;
    string imgName;
}RGBList;

int main(int argc, char **argv){

    RGBList iRGBList;
    ifstream myfile;
    myfile.open(argv[1
],ifstream::in); ofstream outfile("out.txt"); vector<RGBList> vRGBList; string line; size_t comma = 0; size_t comma2=0; size_t comma3=0; string temp,rpg; double timeStamp; vector<string> vstrImageFilenamesRGB; vector<double> vdouTimeStampsRGB; if
(outfile.is_open()) {cout<<"write ready"<<endl;} if(myfile.is_open()) { while (! myfile.eof() ) { getline(myfile,line); if(!line.empty()) { comma=line.find(' ',0); timeStamp=(double)atof(line.substr(0,comma).c_str()); iRGBList.timeStamp=(double
)atof(line.substr(0,comma).c_str()); comma2=line.find('/',comma+1); comma3=line.find('\n',comma2+1); temp=line.substr(comma2+1,comma3); rpg=temp; iRGBList.imgName=line.substr(comma2+1,comma3); cout<<setiosflags(ios::fixed)<<timeStamp<<endl; cout<<rpg<<endl<<endl; vstrImageFilenamesRGB.push_back(rpg); vdouTimeStampsRGB.push_back(timeStamp); vRGBList.push_back(iRGBList); // outfile<<setiosflags(ios::fixed)<<timeStamp<<" "<<rpg<<endl; } } // for (auto iter2=vdouTimeStampsRGB.begin();iter2!=vdouTimeStampsRGB.end();iter2++) // { // outfile<<setiosflags(ios::fixed)<<(*iter2)<<" "; // } // for (auto iter=vstrImageFilenamesRGB.begin();iter!=vstrImageFilenamesRGB.end();iter++) // { // outfile<<(*iter)<<endl; // } // for (auto iter=vRGBList.begin();iter!=vRGBList.end();iter++) // { // outfile<<(*iter)<<endl; // } // for (auto val:vstrImageFilenamesRGB) // { // cout<<endl; // } myfile.close(); outfile.close(); cout<<"io was finished"<<ends; } else cout<<"read fail"<<endl; return 0; }