2

我正在尝试将字符串流式电话查找程序转换为流式文件。我遗漏了一些东西,但我被卡住了。我可以在 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;
}
4

3 回答 3

4

首先,您不需要ofstream, 除非在您打开文件(创建实例)时。输出流接口由std::ostream; std::ofstream派生自此,同样 std::ostringstream(输出可以变成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::ostream 接受一个std::streambuf*作为参数,并且您提供目标类型。

于 2012-11-05T18:05:07.657 回答
0

看起来您不需要将ofstreams 用于此函数的内部部分。事实上,你根本不需要使用流,astd::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;
}
于 2012-11-05T18:05:50.033 回答
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 用于打开和写入文件。该程序有很多语法和逻辑错误,我会在这里阅读更多。

于 2012-11-05T18:06:08.253 回答