繁体   English   中英

无法从文件中读取项目(C ++)

[英]Having trouble reading items from a file (C++)

因此,我在获取文本文件中的最后一项以读取字符串对象时遇到麻烦。

我已经创建了一个名为“ Car”的类,并且应该从文件中读取“ Car”对象的所有参数,但不会注册最后一个。

ifstream对象是“数据”

变量是:

string carType;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;

文本文件中的行显示为:

汽车CN 819481保养假无

这就是我现在所拥有的:

getline(data, ignore); // ignores the header line
data >> carType >> reportingMark >> carNumber >> kind >> loaded;
while (data.peek() == ' ') // this and the next lines were the suggestions of the teacher to bypass the spaces (of which there are more than it will display here)
   data.get();
getline(data, destination);

因此,它将读取“目标”部分以外的所有内容。

该代码似乎是正确的。 除了我不认为需要把

而(data.peek()=='')data.get();

getline(数据,目的地);

部分以读取目的地。 相反,您可以简单地将其读取为数据>>目标。 此外,请检查一下以确保输入文件正确打开

if(data.isOpen()){// cout something}

我希望这有帮助! :)

检查所有IO操作的返回值总是好的。 如果添加错误检查,则可能能够找到问题并找到解决方案。

if (!getline(data, ignore)) // ignores the header line
{
   std::cerr << "Unable to read the header\n";
   exit(EXIT_FAILURE);
}

if ( !(data >> carType >> reportingMark >> carNumber >> kind >> loaded))
{
   std::cerr << "Unable to read the data\n";
   exit(EXIT_FAILURE);
}

while (data.peek() == ' ') // this and the next lines were the suggestions of the teacher to bypass the spaces (of which there are more than it will display here)
   data.get();

if ( !getline(data, destination) )
{
   std::cerr << "Unable to read the rest of the line\n";
   exit(EXIT_FAILURE);
}

怎么样给“ ifstream”对象一个while循环,像这样

    ifstream ifstreamObject;
        ifstreamObject.open("car.txt");


cout << "carType"<< ' '<< "reportingMark" << ' '<< "carNumber" <<' '<< "kind" <<' '<< "loaded"<<' '<<"destination"<< endl;
           while(ifstreamObject >> carType >> reportingMark >> carNumber >> kind >> loaded >> destination )
           {       cout <<"---------------------------------------------------------------------------"<<endl;
                   cout << carType<< ' '<< reportingMark << ' '<< carNumber <<' '<< kind <<' '<< loaded<<' '<<destination<< endl;
           }

问题是这部分:

data >> carType >> reportingMark >> carNumber >> kind >> loaded;

在这里,您尝试修改从流loaded的布尔变量。 您希望读取false可以,但是不可以。 它仅接受01

取而代之的是,不读取布尔变量将切换流的err位,这将导致读取失败后的所有其他内容。

要进行检查,如果在该行之后立即执行data.peek() ,则将收到-1 ,表示没有有效的输入。

要解决此问题,您将需要更改存储信息的方式以存储0/1而不是true/false ,或者更好:

在读取数据之前,先执行: data << boolalpha 这将使流将true/false解释为0/1

如果我是你,我将尝试使用strtok函数从文件中读取。

如果需要,可以阅读以获取更多信息strtok函数

我最近完成了此任务,我使用了strtok,因为它允许将文件的每一行拆分为单词列表。 此外,它还可以避免分配空格等标点符号(因此,我发现它非常有用)

我的例子:我想从文件中读取一些字符数据,例如种族,职业,生命值,攻击和防御。

我文件的每一行看起来都像这样:Human / soldier / 15/7/7

因此,我定义了一个存储strtok函数返回值的char指针和一个存储读取的单词的char指针,直到找到您之前考虑过的定界符。 (在此示例中:“ /”)

char* position = strtok(file, '/');
char* character_info = new char[size];

因此,您将该行存储在character_info中,并在每次迭代中检查位置值,直到完成读取文件为止。

while(position != NULL){
  // store position value
  // call again strtok
}

希望对您有所帮助! =)

干杯

暂无
暂无

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

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