简体   繁体   English

如何在C ++中读取文件时忽略字符

[英]how to ignore a character while reading from a file in c++

I'm trying to write a program that will take a text file I have that has a bunch of contact info in it and store it in an array for later use. 我正在尝试编写一个程序,该程序将使用一个文本文件,该文件中包含一堆联系信息,并将其存储在数组中以备后用。 the text file doesn't have new lines it is just in the format of "Last name", "Last Name, First Name", "Phone-Number", "Email-Address", "Home Address", "Last Name, First Name", "Phone Number","Email Address","","","" 文本文件没有换行符,只是格式为“姓氏”,“姓氏,名字”,“电话号码”,“电子邮件地址”,“家庭住址”,“姓氏,名“,”电话号码“,”电子邮件地址“,”“,”“,”“

So I want to read from the file and take all the data members and save them, the problem is that sometimes it has the name/number inside the "name" and sometimes it just has an empty set of quotes such as "" and occasionally it just has space and another comma like "Phone Number",,"", so i want to be able to check if there is a quote and if there is then read the data till the following " and if there isn't a quote just two consecutive commas then to read nothing into that data field and move onto the next one. 所以我想从文件中读取并获取所有数据成员并保存它们,问题是有时它在“名称”中包含名称/数字,有时它只包含一组空引号,例如“”,有时它只是有空间和另一个逗号,例如“电话号码”,“”,所以我希望能够检查是否有引号,然后再读取数据,直到以下“,并且没有引号只需连续两个逗号即可,然后将任何内容读入该数据字段并移至下一个。

I'm in my junior year of computer science classes in college so i understand somewhat about programing but not everything so try not to blow me out of the water with the correct way. 我在大学三年级的时候就读计算机科学课,所以我对编程有所了解,但对每件事都不是很了解,所以请不要以正确的方式将我从水中引爆。 This is a personal project not any type of homework just so you know. 您知道这是一个个人项目,不是任何类型的作业。

the function i have so far looks like this 我到目前为止的功能看起来像这样

void Info::readFile(ifstream& in)
{
    if (in.good())
    {
        in >> lastName;
    }
    else if (in.bad() || in.fail())
    {
        throw BadException();
    }
    if (in.good())
    {
        in >> lastName;
    }
    else if (in.bad() || in.fail())
    {
        throw BadException();
    }
    if (in.good())
    {
        in >> firstName;
    }
    else if (in.bad() || in.fail())
    {
        throw BadException();
    }
    if (in.good())
    {
        in >> phoneNumber;
    }
    else if (in.bad() || in.fail())
    {
        throw BadException();
    }
    //same code adding other data members
}

First off, all stl stream classes have a cast to bool and operator ! 首先,所有stl流类都有强制转换为bool和operator! that check badbit and failbit for you, so you may replace all 会为您检查故障位和故障位,因此您可以更换所有

if (in.bad() || in.fail())

with

if (!in)

Now for the parsing part, it seems you want to separate the text you get on " and , in a very straightforward way. I suggest you look at std::getline in the string header for that: http://www.cplusplus.com/reference/string/string/getline/ 现在,对于解析部分,似乎您想以一种非常直接的方式将在“和”上获得的文本分开。我建议您在字符串标头中的std :: getline上查看一下: http://www.cplusplus。 com / reference / string / string / getline /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM