2013-12-13 75 views
-2

可以說,我要讀寫這個值 假設我想寫讀寫文件處理C++包含多列

SEAT: 
NAME: 
CLASS: 
DEPR. TIME: 
ARRV. TIME: 
FROM: 
TO: 

============== ========因此,這是將在FILE.DOC =========可以看起來像

SEAT NAME   CLASS  DEPARTURE TIME ARRIVAL TIME FROM DESTINATION 

23 Janes Rowan  ECONOMY 11:30   17:30   NY  CHINA 

24 Robert Sulliman FIRST  12:30   18:30   LONDON JAPAN 

============= ================================================== ==================

而我想讀

請輸入您的姓名:簡氏羅文

在屏幕上顯示============================= === ================================

SEAT NAME   CLASS  DEPARTURE TIME ARRIVAL TIME FROM DESTINATION 

23 Janes Rowan  ECONOMY 11:30   17:30   NY  CHINA 

=========== ================================================== ===================

如何假設我在C++中的編程代碼是?因爲我面臨着寫入問題(保存在file.doc中)和搜索兩個字以上的字符串,並在屏幕上顯示整行。我也想知道如何刪除整行,例如 請輸入您的姓名以取消門票:Janes Rowan 因此,它會刪除整行 我是初學者,因此希望任何人都可以幫助我。非常感謝XD!

回答

0

此代碼符合您的規範,因爲它給(我認爲)。但這不可能是你需要的任務。但也許這裏有一些你可以使用的想法。

我輸入文件,db.txt:

SEAT NAME   CLASS  DEPARTURE TIME ARRIVAL TIME FROM DESTINATION 
23 Janes Rowan  ECONOMY 11:30   17:30   NY  CHINA 
24 Robert Sulliman FIRST  12:30   18:30   LONDON JAPAN 

我的解決辦法,airline.cpp:

#include <algorithm> 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

int main() { 
    // Read the database. 
    std::vector<std::string> database; 
    std::string str; 
    std::ifstream fin("db.txt"); 

    while (std::getline(fin, str)) { 
    database.push_back(str); } 

    // Print the database. 
    for (const auto& r : database) { 
    std::cout << r << "\n"; } 

    // Find. 
    std::cout << "Find name: "; 
    std::string find_query; 
    std::getline(std::cin, find_query); 

    std::cout << database[0] << "\n"; 
    for (const auto& r : database) { 
    if (r.find(find_query) != std::string::npos) { 
     std::cout << r << "\n"; } } 

    // Delete. 
    std::cout << "Delete name: "; 
    std::string delete_query; 
    std::getline(std::cin, delete_query); 

    database.erase(std::remove_if(std::begin(database), std::end(database), 
     [&](const std::string& s) { 
     return s.find(delete_query) != std::string::npos; }), 
     std::end(database)); 

    for (const auto& r : database) { 
     std::cout << r << "\n"; } 
} 

與GCC 4.8.2測試:g++ -Wall -Wextra -std=c++0x airline.cpp