使用getline读取文件时出现问题

Trouble reading file using getline (noob)

本文关键字:问题 文件 getline 读取 使用      更新时间:2023-10-16

我是一个c++新手。我在读取文件到数组结构时遇到了麻烦。这是一个课堂作业,所以我不需要任何人为我做代码,我只想知道我做错了什么。我正在读取的文本文件格式如下:

Giant Armadillo#443#M
Hawaiian Monk Seal#711#M 
Iberian Lynx#134#M
Javan Rhinoceros#134#M etc...

使用getline()可以正确读取带有'#'分隔符的字符串,但不能读取intchar。我如何读取intchar,同时检查分隔符?谢谢,抱歉,如果这不是写清楚,或格式正确。我是新来的。

#include <iostream>
#include<string>
#include <fstream>
#include <cstdlib>
using namespace std;
//Create a new structure
struct Endangered
{   
    string name;
    int population; 
    char species;
};

int main () {
Endangered animals[200];
//initialize animals
for (int i=0; i<50; i++)
{
    animals[i].name=" ";
    animals[i].population=0;
    animals[i].species=' ';
}
ifstream myFile;
myFile.open("animals.txt");
int i=0;
if (myFile.is_open())
{   
    while (myFile.eof())
    {   
        //read the string until delimiter #
        getline(myFile, animals[i].name, '#');
        //read the int until delimiter #
        getline(myFile, animals[i].population, '#');
        //read the char until the delimiter
        getline(myFile, animals[i].species, '/n');
        i++;
    }

    return 0;
}

我敢肯定你不能像这样从文件中读取数据:

//read the int until delimiter #
getline(myFile, animals[i].population, '#');

因为你有一个字符串,你想把它赋值给int或char字段。

看看这个声明:

istream& getline (istream& is, string& str, char delim);

必须有string作为第二个参数。你不能给这个函数传递int或char和animals[i]。Population是int的类型。

你应该首先将数据加载到某个字符串或char*缓冲区,然后使用正确的函数将其转换为你想要的类型。查找像atoi这样的函数。

总结一下。将数据读入临时缓冲区,然后进行转换。

如果这对你没有帮助,我将纠正代码,但你不想:)

程序的读取部分如下所示:

size_t i=0;
ifstream myFile("animals.txt");
if(!myFile)
{
  throw std::runtime_error("Failed opening file");   
}
string line;
while(std::getline(myFile, line))
{
  istringstream line_stream(line);
  string temp;
  if(!std::getline(line_stream, temp, '#'))
    throw std::runtime_error("expected #");
  animals[i].name = std::stoi(temp);
  if(!std::getline(line_stream, temp, '#'))
    throw std::runtime_error(expected #");
  animals[i].population = std::stoi(temp);
  if(!std::getline(line_stream, temp) || temp.size() != 1) // or whatever input verification you need
    throw std::runtime_error("expected species");
  animals[i].species = temp[0]; // or whatever input you need, this assumes species is a char
}

问题是getLine函数返回下一个带分隔符的值作为字符串:函数定义:getline (istream&是,string&

修改代码,将值读入一个临时字符串变量。

然后使用std::stoi(str)将字符串值转换为整数,并将返回值赋给population。

您要求不提供代码,所以我将把实现留给您。

这里有两个问题:读取非空格分隔的文本(包含空格的字符串)和将文本转换为数字。

最简单的方法是:

for(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i)
{   
    char delim = 0; 
    //read the string until delimiter #
    getline(myFile, animals[i].name, '#');
    //read the int 
    myfile >> animals[i].population;
    //read and check delimiter
    if(myfile>>delim && delim!='#') mifile.setstate(myfile.failbit);
    //read the species
    myfile >> animals[i].species;
    //discard any other rubbish untile the end of line
    myfile.ignore(std::numeric_limits<std::streamsize>::max(),'n');
    myfile >> delim; // this is 'n';
}

注意:

  • 我使用for(不是while),以便计数器(i)和它的增量(++i)被定义在一起(没有忘记的风险)。

  • 退出条件不是myfile.eof()(当它发生时已经太晚了),通过myfile(它可以自转换为"true"如果"可读":如果文件变得不可读,你退出,无论错误,即使还没有结束)

  • 另一个退出条件(in &&第一个)表示数组已经被完全填充(表达式就是它的大小,不管它是什么)

    • 第一个字符串(可能包含空格)被读取到'#'(即读取,但不存储,根据您的需要)

    • 将数字作为输入文件读取(如果没有数字,>>操作符将设置failbit,您将不再读取并安全退出)

    • 分隔符被读取为输入字段:数字之后和#之前的任何空格都被丢弃。#进入delim,如果我们发现它不是#,我们设置failbit(文件不包含应该包含的内容)并阻止任何读取并安全退出循环(myfile将评估为false)。

    • 每个最终空格都被丢弃,下一个字符进入species

    • 现在让我们丢弃任何最终留在在线上的东西(包括'n')。

    • 我们现在可以继续下一行了

请注意,您的包装if是不需要的:如果myfile没有打开,它的计算结果为"false",并且不会进入循环。

eof()条件,在这里,不能被看作是"失败终止",而是"达到成功"。

如果-after loop- file.eof()为true,则表示已成功读取所有内容,否则由于错误而过早停止。

还要注意,整个for函数体可以是名为

的函数体。
std::isream& operator>>(std::istream& myfile, Endangered& endangered)
{
  .....
  .....
  return myfile;
}

,循环将只是

for(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i)
   myfile >> animals[i];