注册 登录
编程论坛 C++教室

求助:C++文件操作: 换行 定位修改

小颭 发布于 2011-07-17 08:37, 719 次点击
list.txt
Tom 18 50 1.76
Bob 18 52 1.74
Kyle 18 55 1.79
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int fileToVector(string fileName, vector<string> & svec)
{
    ifstream inFile(fileName.c_str());
    if (!inFile)
        return 1;

    string s;
    while (getline(inFile, s))
        svec.push_back(s);
    inFile.close();
    if(inFile.eof())
        return 4;
    if(inFile.bad())
        return 2;
    if(inFile.fail())
        return 3;
}

int main()
{
    vector<string> svec;
    string fileName("E:\\list.txt"), s;

    switch (fileToVector(fileName, svec)) {
    case 1:
        cout << "error: can not open file: "
             << fileName << endl;
    case 2:
        cout << "error: system failure " << endl;
    case 3:
        cout << "error: read failure " << endl;
    return 0;

    return -1;
    }

    cout << "Vector:" << endl;
    for(vector<string>::iterator iter = svec.begin();
        iter != svec.end(); ++iter)
        cout << *iter << endl;
      
    return 0;
}

运行环境:VC++6.0

请问:
如果要定位到Bob的体重:52,并修改它,要怎么实现




[ 本帖最后由 小颭 于 2011-7-17 09:39 编辑 ]
3 回复
#2
specilize2011-07-17 21:29
可以用vector中的元素(假设为某一元素s)建立一个istringstream变量,这样就可以读入s中的内容,即
Bob 18 52 1.74为svec[1],建立istringstream iss(svec[i]),使用iss>>s(s为string变量)即可依次读入Bob 18.....
#3
guyangjie2011-07-21 07:19
观摩观摩本人是新手啊
#4
lucky5635912011-07-24 10:03
读入内存中修改对象属性。
1