【问题标题】:Is there a way to read in data after a specified word is encountered in a file?在文件中遇到指定单词后,有没有办法读入数据?
【发布时间】:2014-02-18 02:25:19
【问题描述】:

我正在创建的程序将从包含大量地址和邮政编码的文本文件中读取数据。

我的问题是:每次文件读取“zip:”(if(text == "zip:") 时,程序应该打印出它后面的令牌(规范要求面向令牌的输入),这意味着邮政编码。

是否有某种功能只会打印出邮政编码,而不会打印出邮政编码之后的其他文本?很抱歉,这篇长文只是想尽可能详细地介绍该程序。如果有任何其他信息我应该包括,请告诉我。我不是在找人给我完整的程序,只是对这个特定问题的一些指导将不胜感激。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main() {
    string text;
    ifstream inFile;
    inFile.open("zips");
    while(!inFile.fail()) {
        inFile >> text;
        if(text == "zip:") {

        }  
    }   
    inFile.close();
    return 0;
}  

输入是被循环的文件本身,用户没有输入任何输入。 我想要的输出是前十个最常见的邮政编码。 例如:

Zipz:   Frequency:
11204      39
11234      33
22098      27....etc.

这里是一些文件包含的示例。

cc: visa addr: 488 Cicada Avenue =4=Z city: Edmonton zip: T5T4M4 $20.00 cc: visa addr: 48030 Nevada Blvd =4=Z city: Montecito 邮编: 95041 $15.00 cc: visa addr: 493 Park Meadow Drive =4=Z city :阿拉莫邮编:94521 $10.00 cc: mastercard addr: 893 Moraga Avenue =4=Z city: San Bruno 邮编: 94012 $15.00

【问题讨论】:

  • 您能否展示一些示例输入行以及您期望的输出?
  • 输入的是文件本身,用户不会输入任何东西。
  • 从文件中输入,就像从中提取的样本行一样。
  • 他的意思是发布一个输入文件的样本,它包含什么,以及你期望的输出从它给你的程序。
  • 你能告诉我们文件里面到底是什么吗??

标签: c++ file-io


【解决方案1】:

假设您的输入文件将始终按照您发布的格式进行格式化,这意味着 zip 始终会有一个值(我没有检查极端情况),应该这样做:

#include <iostream>
#include <string>
#include <fstream>
#include <map>

using namespace std;

int main()
{
    ifstream inFile;
    inFile.open("test");

    string text;

    map<string, int> frequencies;

    while (!inFile.fail())
    {
        inFile >> text;

        if (text == "zip:" && !inFile.fail())
        {
            string zip;
            inFile >> zip;

            if (frequencies.find(zip) == frequencies.end())
                frequencies[zip] = 1;
            else
                frequencies[zip]++;
        }
    }

    map<string, int>::iterator it = frequencies.begin();
    while (it != frequencies.end())
    {
        cout << (*it).first << ": " << (*it).second << endl;
        ++it;
    }

    return 0;
}

在您的示例文件上运行 1 个副本并得到以下输出:

94012: 1
94521: 1
95041: 2
T5T4M4: 1

虽然格式和排序缺失。排序可以通过将地图中的值放入支持排序的容器中来实现,例如setvector

看看这些答案,看看它是如何做到的:

矢量:https://stackoverflow.com/a/8640935/109960
设置:https://stackoverflow.com/a/2699101/109960

【讨论】:

    【解决方案2】:

    好吧,根据您在上面提供的文件,我将以这种方式解决这个问题。我不会提供经过现场测试的实际 C++ 代码,而是提供一个通用过程。

    首先,我将创建一个数据结构来汇总我们可以获得的所有信息。

    // Store all the zip codes
    std::vector<int> codes;
    

    然后我将开始逐字符读取文件。

      std::ifstream is(str);     // open file
    
      while (is.good())          // loop while extraction from file is possible
      {
        char c = is.get();       // get character from file
        if (is.good())
        {
          if(c == 'z')
            if(is.get() == 'i')
              if(is.get() == 'p')
                if(is.get() == ':')
                {
                  // Extract the next 6 characters from the stream
                  // and store them as a string or something
                  // which you can later convert into an integer
                  // and push into the data structure we created earlier
                }
        }
    
      }
    
      is.close();                // close file
    

    您可以稍后计算向量中每个邮政编码的出现次数,然后将有关此类的信息存储在 std::map 中。

    【讨论】:

      猜你喜欢
      • 2022-10-07
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多