2012-11-05 33 views
2

我想將字符串流的手機查找程序轉換成文件流..我錯過了一些東西,但我卡住了..什麼成員我可以在ofstream過程中得到這工作?C++從文件讀取向量

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

你沒有將你的流與文件關聯起來。您需要調用'open()'並傳遞文件名(或使用適當的構造函數)。並且流沒有名爲'empty()'的成員函數。 – jrok

回答

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::ostreamstd::streambuf*作爲參數,並且您提供目標 類型。

0

看起來你不需要使用ofstream s作爲這個函數的內部部分。事實上,你並不需要使用流可言,一個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; 
} 
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是用於打開和寫入文件。該程序有很多語法和邏輯錯誤,我會在其上閱讀更多here