试图弄清楚如何使用 getline() 将项目读入文件:一种方法有效但另一种方法无效

问题描述

请检查位于代码中间的区域,该区域指出:这是我正在努力的区域。我只是对如何为 For_Struggling.txt 做到这一点感到困惑。如果我可以使用 getline(),那会很有帮助,我只是继续得到:ADTExample.cpp:57:10: 错误:没有匹配的函数来调用 'getline'

在终端上!任何帮助都会很棒!

For_Struggling.txt 文件:46500,香蕉,0.50

Non_Struggling.txt 文件:46500 Banana 0.50(这是给被屏蔽的)

#include <iostream>
#include <fstream>
#include <string>
#include <vector> //We need this to store things in
#include <iomanip>

using namespace std;

struct Item {
    int SKU;
    string name;
    float price;
};

void printItem(Item item);

int main(int argc,char** argv)
{
    //Make sure the user is using the program correctly
    if (argc != 2)
    {
        cout << "ERROR: Incorrect argument count.\nUsage ./a.out <input_filename>" << endl;
        return 1;
    }




    ifstream listFile;
    listFile.open(argv[1]); //Used the command line argument filename to open the file
    //If the file didn't open correctly,print an error message and terminate
    if (!listFile.good())
    {
        cout << "Could not open file: " << argv[1] << endl << "Is it in the correct directory?" << endl;
        return 1;
    }


//////////////////////This is the area I am struggling in //////////////////


    //Read the items into the file
    vector<Item> itemVector;
    while (true)
    {
        Item temp; //Item to read into
        int read_line; 
        ifstream listFile;
        //trying to figure out if I can read it this way 

       listFile >>  getline(listFile,temp.SKU,',') >> getline(listFile,temp.name,temp.price); 
        //Read the line

        // It works this way ------>>>> listFile >> temp.SKU >> temp.name >> temp.price;

//Why doesn't the the other way work? 


        if (listFile.eof()) //If we're at the end of file,stop reading
            break;
        itemVector.push_back(temp); //Puts the item at the end of the vector
    }


//////////////////////////////////////////////////

    //Print all of the items that we read and how many we read
    cout << "Read " << itemVector.size() << " items!" << endl << endl;
    for (int i = 0; i < itemVector.size(); i++)
        printItem(itemVector[i]);

    listFile.close();
    return 0;
}

    //Prints the members of the given struct
    void printItem(Item item)
    {
        cout << "SKU: " << item.SKU << endl;
        cout << "Name: " << item.name << endl;
        cout << "Price: $" << fixed << setprecision(2) << item.price << endl;
        cout << endl;
    }

解决方法

在以下代码行中,您尝试同时使用 std::istream 的多个功能:

  • std::istream 实例上的右移运算符 (">>") 的作用就像您将某些东西从给定的 std::istream 推送到右侧变量一样;
  • std::getline 正在读取给定的 std::istream 并返回一个 std::istream;
listFile >>  getline(listFile,temp.SKU,',') >> getline(listFile,temp.name,temp.price); 

如此处所写,您试图将从文件中读取的内容写入 std::ifstream (或者,更具体地说,写入 std::basic_istream&,这是 std 的返回类型: :geline 根据 cppreference page for std::getline );

只是做:

std::getline(listFile,');
std::getline(listFile,temp.price);

应该完成这项工作;

或者,如果您愿意,作为“std::istream”方法,它的正确用法应该是:

char* charBuffer;
long long desiredStreamSize = 100;
char delim  = ',';

int myProperty;

listFile.getline(charBuffer,desiredStreamSize,delim);

myProperty = std::stoi(charBuffer);

以整数属性为例。

,

基本上,getline 是一个函数,用于在 std::string
例如

std::string word;
getline(std::cin,word);

这是从文件中为您的案例使用 getline 的解决方案,
#include <iostream>
#include <fstream>
#include <string>

struct Item {
    int SKU;
    std::string name;
    float price;
};

int main(){
    // put your filename at file.txt
    // 46500,Banana,0.50
    Item item;
    std::ifstream file("file.txt"); 
    std::string temp;
    getline(file,temp,');
    item.SKU = std::stoi(temp); // change from string to integer
    getline(file,item.name,'); // no need to change anything since its the same
    getline(file,temp);
    item.price = std::stod(temp); // change from string to double/float

    std::cout << item.SKU << std::endl;
    std::cout << item.name << std::endl;
    std::cout << item.price << std::endl;
}

输出:

46500
 Banana
0.5

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...