在输入文件中查找字符串 - C ++

时间:2015-04-07 20:20:31

标签: c++

我需要在文本文件中找到用户输入的字符串(链接名称)。 如何在c ++中使用解决方案?我是否必须将文件上下文存储在结构中以便以后读取数据?或者我可以随时打开并阅读文件,只要我想查找信息? 谢谢!

输入文件示例

111.176.4.191 www.yahoo.com 01/04/2013
111.176.4.191 www.yahoo.com 01/09/2013
192.168.1.101 www.yahoo.com 01/04/2013
111.176.4.191 www.yahoo.com 01/12/2013
192.168.1.101 www.espn.com 01/05/2013

C ++代码

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <string>
using namespace std;


//gobal variables, procedures

void fileinfo1(string);

char IP_Address [12];

char Link_Name [50];

char Date_Accessed [8];

string filename;

int menu;


int main()
{
    // the user will input the file name here
    cout << "Enter filename> ";

    getline( cin, filename );

    fstream file( filename.c_str() );

    if (!file)

    {

        cout << "Invalid file.\n";


        return EXIT_FAILURE;
    }

    // the program will display the file context
    else
    {
        string line;
        int count = 10;
        while ((count > 0) && getline( file, line ))
        {
            cout << line << '\n';
            count--;
        }
        file.close();
    }
    return EXIT_SUCCESS;

    // The user will be able to choose to see info about all entries or a particular one

    cout << "Please select a menu option:";
    cout << "1)Link Information in date range";
    cout << "2)Information about all links";
    cout << "3)Quit the program";
    cin >> menu;

    switch (menu) {

    // see info about a particular link
    case 1: fileinfo1(filename);
        break;

    case 2:
        break;

    case 3:
        break;

    default: cout << "Please a choose a number between 1 and 3";
        break;

    }

    // the file is passed to this function
    void fileinfo1(string filename) {

        //the user will input a link e.g www.espn.com
        cout << "What is the link name? ";

        cin >> Link_Name;

        // and also input date range (start-end)
        cout << "What is the starting date? " ;

        cin >> Date_Accessed;

        cout << "What is the ending date? " ;

        cin >> Date_Accessed;

        // Now, here's where I'm having trouble

        // I need to find the wwww.espn.com in my file based on the range date , so that i will be able to increment the number of hits

        unsigned int curLine = 0;
        while (getline(filename, line)) { // I changed this, see below
            curLine++;
            if (line.find(search, 0) != string::npos) {
                cout << "found: " << search << "line: " << curLine << endl;
            }
        }

    }

}

谢谢!

2 个答案:

答案 0 :(得分:0)

这部分代码不应写入main()函数。

// the file is passed to this function
void fileinfo1(string filename) {

    //the user will input a link e.g www.espn.com
    cout << "What is the link name? ";

    cin >> Link_Name;

    // and also input date range (start-end)
    cout << "What is the starting date? " ;

    cin >> Date_Accessed;

    cout << "What is the ending date? " ;

    cin >> Date_Accessed;

    // Now, here's where I'm having trouble

    // I need to find the wwww.espn.com in my file based on the range date , so that i will be able to increment the number of hits

    unsigned int curLine = 0;
    while (getline(filename, line)) { // I changed this, see below
        curLine++;
        if (line.find(search, 0) != string::npos) {
            cout << "found: " << search << "line: " << curLine << endl;
        }
    }

}

并且您正在使用许多全局变量,而这些变量实际上并不是必需的。并且你dident声明变量行和搜索。这甚至可以编译。

答案 1 :(得分:0)

你想要一个快速而肮脏的解决方案还是一个优雅的解决方案?

为了获得优雅的解决方案,我会:

  • 抛弃全局。
  • 在解析之前将整个文件读入内存。
  • 为您的数据生成内部数据库。
  • 编写一些返回数据子集的查询函数。

对于您的特定情况,您可以使用std :: multimap&lt; LinkName,DateAndIP&gt;查找与链接相关的所有数据。 DateAndIP可以是std :: multimap&lt;的typedef。日期,IP&gt; 。如果您从未使用过multimap,这将是一次很好的学习体验。编写比较函数并使用查找成员函数仅返回您要查找的内容。

祝你好运,编码愉快!