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;
}  
 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件(译者注:在标准C++中,已经使用取 代,所有的C++标准头文件都是无后缀的。)。这是 的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, 已经被包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作类的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台流"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个类对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了流操作,对 "<>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 流很像. 和刚刚讨论的输出流很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往流里插入变量和符号吧?好,?在 "<>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值