使用getline(noob)读取文件时出现问题 我是一名NoOB C++学生。我无法将文件读入数组结构。这是一个课堂作业,所以我不需要任何人为我做代码,我只想知道我做错了什么。我正在读取的文本文件的格式如下: Giant Armadillo#443#M Hawaiian Monk Seal#711#M Iberian Lynx#134#M Javan Rhinoceros#134#M etc...

使用getline(noob)读取文件时出现问题 我是一名NoOB C++学生。我无法将文件读入数组结构。这是一个课堂作业,所以我不需要任何人为我做代码,我只想知道我做错了什么。我正在读取的文本文件的格式如下: Giant Armadillo#443#M Hawaiian Monk Seal#711#M Iberian Lynx#134#M Javan Rhinoceros#134#M etc...,c++,string,syntax,getline,C++,String,Syntax,Getline,使用getline()可以正确读取带有“#”分隔符的字符串,但不能使用int或char。检查分隔符时如何读取int或char? 谢谢,如果写得不清楚或格式不正确,很抱歉。我对SO是全新的 #include <iostream> #include<string> #include <fstream> #include <cstdlib> using namespace std; //Create a new structure struct End

使用
getline()
可以正确读取带有
“#”分隔符的字符串,但不能使用
int
char
。检查分隔符时如何读取
int
char
? 谢谢,如果写得不清楚或格式不正确,很抱歉。我对SO是全新的

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
//创建一个新结构
结构濒危
{   
字符串名;
国际人口;
炭种;
};
int main(){
濒危动物[200];
//初始化动物

对于(int i=0;i我确信您不能像这样从文件中读取数据:

//read the int until delimiter #
getline(myFile, animals[i].population, '#');
因为您得到了sting,并且希望将其分配给int或char字段

看看这个声明:

istream& getline (istream& is, string& str, char delim);
必须将字符串作为第二个参数。不能将int或char和animals[i]传递给此函数。人口是int的类型

您应该首先将数据加载到某个字符串或char*缓冲区,然后使用正确的函数将其转换为所需的类型

将数据读入临时缓冲区,然后进行转换


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

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

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函数以字符串形式返回下一个分隔值: 函数def:getline(istream&is、string&str、char-delim)

更改代码以将值读入临时字符串变量

然后使用std::stoi(str)将字符串值转换为整数,并将返回值分配给填充

您要求不提供任何代码,因此我将把实现留给您


希望这有帮助。

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

快速和肮脏的方式可以是:

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(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i)
   myfile >> animals[i];
for(inti=0;myFile&&i>animals[i];

使用
'\n'
而不是
'/n'
这是非常有用的。谢谢!!如果(我的文件>>h&&h!='.#')mifile.setstate(myfile.failbit);@dana\u muise:oops…输入错误:它应该是
delim
。刚刚编辑过。
for(int i=0; myFile && i<sizeof(animals)/sizeof(Endangered); ++i)
   myfile >> animals[i];