C ++匹配文件中的字符串并获取行号 [英] C++ match string in file and get line number

查看:89
本文介绍了C ++匹配文件中的字符串并获取行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,上面有1000个婴儿的名字.我想问用户一个名字...搜索文件...并告诉用户该名字代表男孩名字的等级和女孩名字的等级.如果不是男孩名或女孩名,它会告诉用户它不在该性别的流行名称之列.

I have a file with the top 1000 baby names. I want to ask the user for a name...search the file...and tell the user what rank that name is for boy names and what rank for girl names. If it isn't in boy names or girl names, it tells the user it's not among the popular names for that gender.

文件的布局如下:

Rank Boy-Names Girl-Names
1    Jacob     Emily
2    Michael   Emma
.
.
.

输入Michael的所需输出为:

迈克尔在男孩名字中排名第二.

Michael is 2nd most popular among boy names.

如果Michael不在女孩名字中,则应说:

If Michael is not in girl names it should say:

迈克尔不在最受欢迎的女孩名字中

Michael is not among the most popular girl names

尽管是这样,但它会说:

Though if it was, it would say:

米歇尔在女孩名字中(排名)

Micheal is (rank) among girl names

我到目前为止的代码在下面..我似乎无法弄清楚.感谢您的帮助.

The code I have so far is below.. I can't seem to figure it out. Thanks for any help.

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void find_name(string name);

int main(int argc, char **argv)
{
    string name;
    cout << "Please enter a baby name to search for:\n";
    cin >> name;
    /*while(!(cin>>name))
    {
        cout << "Please enter a baby name to search for:\n";
        cin >> name;
    }*/
    find_name(name);

    cin.get();
    cin.get();
    return 0;
}

void find_name(string name)
{
    ifstream input;
    int line = 0;
    string line1 = " ";
    int rank;
    string boy_name = "";
    string girl_name = "";
    input.open("/<path>/babynames2004.rtf");
    if (!input)
    {
        cout << "Unable to open file\n";
        exit(1);
    }

    while(input.good())
    {
        while(getline(input,line1))
        {
            input >> rank >> boy_name >> girl_name;
            if (boy_name == name)
            {
                cout << name << " is ranked " << rank << " among boy names\n";
            }
            else
            {
                cout << name << " is not among the popular boy names\n";
            }
            if (girl_name == name)
            {
                cout << name << " is ranked " << rank << " among girl names\n"; 
            }
            else
            {
                cout << name << " is not among the popular girl names\n";
            }
        }
    }
    input.close();
}

推荐答案

首先,您似乎正在尝试打开富文本格式文件(.rtf).这将不起作用,因为文件不仅包含文本,还包含其他数据( http://en .wikipedia.org/wiki/Rich_Text_Format ).

First off, looks like you're trying to open a rich text format file (.rtf). This won't work, as the file doesn't contains only text but also other data (http://en.wikipedia.org/wiki/Rich_Text_Format).

然后在您的代码中:while(getline(input,line1))每次迭代读取一行.很好,但是在循环中执行input >> rank >> boy_name >> girl_name;,它会继续在下一行中读取

Then in your code: while(getline(input,line1)) reads a line every iteration. That's fine, but inside the loop yo do input >> rank >> boy_name >> girl_name; which continues to read in next line

您想使用line1.您可以从第1行构造一个字符串流,然后从中读取名称:

You want to work with line1. You can construct a stringstream from line1, then read the names from it:

stringstream ss(line1):
ss >> rank >> boy_name >> girl_name;

那和Beta在他的回答中写的一样;您会在名称不匹配的每一行中放弃".

That and what Beta wrote in his answer; you "give up" in each line where names don't match.

这篇关于C ++匹配文件中的字符串并获取行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆