【问题标题】:C++ Read from file to vectorC++ 从文件读取到向量
【发布时间】:2012-10-25 14:35:19
【问题描述】:

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

【问题讨论】:

  • 您没有将流与文件相关联。您需要调用open() 并传递文件名(或使用适当的构造函数)。而且流没有名为empty()的成员函数。

标签: c++ file ifstream ofstream


【解决方案1】:

首先,您不想要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::ostreamstd::streambuf* 作为参数,并提供目的地 输入。

【讨论】:

    【解决方案2】:

    您永远不会将文件与 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 用于打开和编写文件。该程序有很多语法和逻辑错误,我会更多地阅读它here

    【讨论】:

      【解决方案3】:

      看起来您不需要将ofstreams 用于此函数的内部部分。事实上,你根本不需要使用流,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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-04
        • 1970-01-01
        • 1970-01-01
        • 2019-03-01
        相关资源
        最近更新 更多