C ++从文件读取到向量

时间:2012-11-05 17:46:52

标签: c++ file ifstream ofstream

我正在尝试将字符串流式电话查找程序转换为文件流式传输..我遗漏了一些东西,但我卡住了...我可以在ofstream流程中使用哪些成员来使其正常工作?

ofstream& process (ofstream &os, vector<PersonInfo> people)
{
    // for each entry in people
    for (vector<PersonInfo>::const_iterator entry = people.begin();
                entry != people.end(); ++entry) {    
        ofstream formatted, badNums; // objects created on each loop

        // for each number
        for (vector<string>::const_iterator nums = entry->phones.begin();
                nums != entry->phones.end(); ++nums) {  
            if (!valid(*nums)) {           
                badNums << " " << *nums;  // string in badNums
            } else                        
                // ``writes'' to formatted's string
                formatted << " " << format(*nums); 
        }
        if (badNums.empty())      // there were no bad numbers
            os << entry->name << " "    // print the name 
               << formatted.str() << endl; // and reformatted numbers 
        else                   // otherwise, print the name and bad numbers
            cerr << "input error: " << entry->name 
                 << " invalid number(s) " << badNums.str() << endl;
    }

    return os;
}

3 个答案:

答案 0 :(得分:4)

首先,您不想要ofstream,除非您正在打开这一点 文件(创建实例)。输出流接口是 由std::ostream定义; std::ofstream也是如此。std::ostringstream {{3}}(输出可以成为std::string),并且在大多数情况下 应用程序,由本地程序员编写的其他几个。在 你的情况(如果我能正确理解问题),你想要的是:

std::ostream& process( std::ostream& os,
                       std::vector<PersonInfo> const& people )
    //  Note the use of a const reference above.  No point
    //  in copying the entire vector if you're not going to
    //  modify it.
{
    for ( std::vector<PersonInfo>::const_iterator entry = people.begin();
            entry != people.end();
            ++ entry ) {
        std::ostringstream formatted;
        std::ostringstream badNums;
        //  ...
        if ( badNums.str().empty() ) {
            os << ... << formatted.str() << std::endl;
        } else {
            os << ... << badNums.str() << std::endl;
        }
    }
    return os;
}

请注意不同的类型:std::ostream格式输出,独立 目的地类型。 std::ofstream源自它,并提供 一个文件作为目的地。 std::ostringstream源自它,和 提供std::string作为目标类型。 std::ostreamstd::streambuf*为参数,并提供目的地 类型。

答案 1 :(得分:0)

看起来您不需要将ofstream用于此功能的内部部分。实际上你根本不需要使用流,std::string会这样做:

ofstream& process (ofstream &os, vector<PersonInfo> people)
{
    // for each entry in people
    for (vector<PersonInfo>::const_iterator entry = people.begin();
                entry != people.end(); ++entry) {    
        string formatted, badNums; // objects created on each loop

        // for each number
        for (vector<string>::const_iterator nums = entry->phones.begin();
                nums != entry->phones.end(); ++nums) {  
            if (!valid(*nums)) {           
                badNums += " " + *nums;  // string in badNums
            } else                        
                // ``writes'' to formatted's string
                formatted += " " + format(*nums); 
        }
        if (badNums.empty())      // there were no bad numbers
            os << entry->name << " "    // print the name 
               << formatted << endl; // and reformatted numbers 
        else                   // otherwise, print the name and bad numbers
            cerr << "input error: " << entry->name 
                 << " invalid number(s) " << badNums << endl;
    }

    return os;
}

答案 2 :(得分:0)

您永远不会将文件与ostream关联,因此编译器不知道如何处理您写入的数据。

ofstream& process (ofstream &os, vector<PersonInfo> people)
{
    os.open("Data.txt"); //open file to be used
    if(!os.is_open())
        std::cerr << "Error opening file!\n";
    //rest of code goes here
}
编辑:再次阅读你的程序后,我注意到你使用了ofstream错误。 Ofstream用于打开和编写FILES。该程序有很多语法和逻辑错误,我会更多地阅读here

相关问题