【问题标题】:read and write file processing c++ which contains multiple column [closed]包含多列的读写文件处理c++[关闭]
【发布时间】:2013-12-13 02:25:40
【问题描述】:

假设我想写入和读取这个值 假设我想写

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!

【问题讨论】:

    标签: c++ file-processing ticket-system


    【解决方案1】:

    此代码符合您给出的规范(我认为)。但这不太可能是您的任务所需要的。但也许这里有一些你可以使用的想法。

    我的输入文件,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

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多