用分隔符读入文件 [英] Reading in file with delimiter

查看:111
本文介绍了用分隔符读入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从文件中读取行并将该行的特定段分配给结构中的信息?我怎么能停在一个空行,然后再继续,直到文件结束?

背景:我正在建立一个程序,将采取一个输入文件,阅读在信息中,并使用双重散列的信息被放在正确的哈希表索引。

假设我有结构:

  struct Data 
{
string city;
字符串状态;
string zipCode;
};

但文件中的行格式如下:

  20 

85086,亚利桑那州凤凰城
56065,明尼苏达州明尼阿波利斯

85281
56065

对不起,我仍然无法弄清楚。我正在阅读文件非常困难。第一行基本上是要构建的哈希表的大小。下一个空白行应该被忽略。然后接下来的两行是应该进入结构并散列到哈希表中的信息。然后另一个空白行应该被忽略。最后,最后两行是需要匹配的输入,看它们是否存在于哈希表中。所以在这种情况下,找不到85281。 56065被找到。

解决方案

另外两个答案指出你必须使用 std :: getline ,但这是我的方式:


$ b

if(std :: getline(是,zipcode,',')&&
std :: getline(是,city,',')&&
std :: getline (is,state))
{
d.zipCode = std :: stoi(zipcode);



$ b $ p
$ b

我唯一真正的变化是我把提取包在一个 if 语句,所以你可以检查这些读取是否成功。而且,为了方便这个(你不想为每个 Data 对象输入上面的内容),你可以把它放在一个函数中。 p>

您可以为 Data 重载>> > class like like:

std :: istream& (std :: istream&是,Data& d)
{
std :: string zipcode;
if(std :: getline(is,zipcode,',')&&
std :: getline(is,d.city,',')&&
std :: getline(is,d.state))
{
d.zipCode = std :: stoi(zipcode);
}

return is;



$ b现在它变得如此简单:

Data d;

if(std :: cin>> d)
{
std :: cout<< 是的,它的工作!;
}


How do I read in lines from a file and assign specific segments of that line to the information in structs? And how can I stop at a blank line, then continue again until end of file is reached?

Background: I am building a program that will take an input file, read in information, and use double hashing for that information to be put in the correct index of the hashtable.

Suppose I have the struct:

struct Data
{
    string city;
    string state;
    string zipCode;
};

But the lines in the file are in the following format:

20

85086,Phoenix,Arizona
56065,Minneapolis,Minnesota

85281
56065

Sorry but I still cannot seem to figure this out. I am having a really hard time reading in the file. The first line is basically the size of the hash table to be constructed. The next blank line should be ignored. Then the next two lines are information that should go into the struct and be hashed into the hash table. Then another blank line should be ignored. And finally, the last two lines are input that need to be matched to see if they exist in the hash table or not. So in this case, 85281 is not found. While 56065 is found.

解决方案

As the other two answers point out you have to use std::getline, but this is how I would do it:

if (std::getline(is, zipcode, ',') &&
    std::getline(is, city,   ',') &&
    std::getline(is, state))
{
    d.zipCode = std::stoi(zipcode);
}

The only real change I made is that I encased the extractions within an if statement so you can check if these reads succeeded. Moreover, in order for this to be done easily (you wouldn't want to type the above out for every Data object), you can put this inside a function.

You can overload the >> operator for the Data class like so:

std::istream& operator>>(std::istream& is, Data& d)
{
    std::string zipcode;
    if (std::getline(is,  zipcode, ',') &&
        std::getline(is, d.city,   ',') &&
        std::getline(is, d.state))
    {
        d.zipCode = std::stoi(zipcode);
    }

    return is;
}

Now it becomes as simple as doing:

Data d;

if (std::cin >> d)
{
    std::cout << "Yes! It worked!";
}

这篇关于用分隔符读入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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