使用getline从c ++中的文件中读取

using getline to read from file in c++

我现在正在编写一个程序,我正在尝试从文件中读取输入并将成员放入变量中。当我尝试使用 getline 时,它​​给我一个错误。这是我试图从中读取的文本文件中的行。

pontoon,Crest,Carribean RS 230 SLC,1,Suzuki,115,Blue,26,134595.00,135945.00

这是我的构造函数代码:

watercraft::watercraft(ifstream &inFile){
    //use getline
    //stoi to convert string to int
    
    char tempType[15];
    char tempMake[20];
    char tempModel[30];
    char tempPropulsion[15];            //int
    char tempEngine[15];
    char tempHp[15];                    //int
    char tempColor[25];
    char tempLength[15];                 //int
    char tempBase_price[15];            //double
    char tempTotal_price[15];           //double
    
    getline(inFile, tempType, ',');
    getline(inFile, tempMake, ',');
    getline(inFile, tempModel, ',');
    getline(inFile, tempPropulsion, ',');
    getline(inFile, tempEngine, ',');
    getline(inFile, tempHp, ',');
    getline(inFile, tempColor, ',');
    getline(inFile, tempLength, ',');
    getline(inFile, tempBase_price, ',');
    getline(inFile, tempTotal_price, ',');
    
    cout << tempType << endl;
}

这是我的 mainDriver 代码:

int main(int argc, const char * argv[]){
    int choice;
    int numFor1 = 0;
    ifstream inFile("watercraft.txt");
    if(!inFile){
        cout << "Something wrong with input file" << endl;
        exit(0);
    }
    
    do{
        choice = printMenu();       //call print menu and assign user input to choice
        switch(choice){         //switch statement to do as the user pleases
            case 1:
                if(numFor1 == 0){       //if file hasnt been initialized
                    watercraft test(inFile);
                    numFor1++;
                }else{
                    printf("You have already Initialized list\n");      //if file has been initialized
                }
                break;
        }
    }while(choice != 12);       //do while user does not want to exit the program
    return 0;
}

我遇到了很多错误。这只是一块错误消息,似乎在 getline.

的每次迭代中重复出现
watercraft.cpp:32:5: error: no matching function for call to 'getline'
    getline(inFile, tempType, ',');
    ^~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h:355:9: note: 
      candidate function not viable: no known conversion from
      'std::__1::ifstream' (aka 'basic_ifstream<char>') to 'char **' for 1st
      argument
ssize_t getline(char ** __restrict __linep, size_t * __restrict __lineca...
        ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/istream:1507:1: note: 
      candidate template ignored: could not match
      'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>'
      against 'char [15]'
getline(basic_istream<_CharT, _Traits>& __is,

编译器错误指的是 <stdio.h> 中的 C getline() 函数,该函数用于从 C FILE* 流中读取一行作为 char* 字符串。您正在使用 C++ std::ifstream,该版本的 getline() 不支持。

此外,也没有任何版本的 C++ std::getline() 函数可以将行从 std::ifstream 读取到 char[] 缓冲区。

然而,std::ifstream 派生自 std::istream,它确实有一个 getline() 方法来将一行读入 char[] 缓冲区。这就是您的构造函数需要调用的内容,例如:

watercraft::watercraft(ifstream &inFile){
    //use getline
    //stoi to convert string to int
    
    char tempType[15];
    char tempMake[20];
    char tempModel[30];
    char tempPropulsion[15];            //int
    char tempEngine[15];
    char tempHp[15];                    //int
    char tempColor[25];
    char tempLength[15];                //int
    char tempBase_price[15];            //double
    char tempTotal_price[15];           //double
    
    inFile.getline(tempType, 15, ',');
    inFile.getline(tempMake, 20, ',');
    inFile.getline(tempModel, 30, ',');
    inFile.getline(tempPropulsion, 15, ',');
    inFile.getline(tempEngine, 15, ',');
    inFile.getline(tempHp, 15, ',');
    inFile.getline(tempColor, 25, ',');
    inFile.getline(tempLength, 15, ',');
    inFile.getline(tempBase_price, 15, ',');
    inFile.getline(tempTotal_price, 15, ',');
    
    cout << tempType << endl;
}

虽然 std::getline() 会更好,特别是如果您要将字符串传递给 std::stoi()std::stod(),它们需要 std::string 输入,而不是 char[] 输入:

watercraft::watercraft(ifstream &inFile){
    //use getline
    //stoi to convert string to int
    
    std::string tempType;
    std::string tempMake;
    std::string tempModel;
    std::string tempPropulsion;            //int
    std::string tempEngine;
    std::string tempHp;                    //int
    std::string tempColor;
    std::string tempLength;                //int
    std::string tempBase_price;            //double
    std::string tempTotal_price;           //double
    
    std::getline(inFile, tempType, ',');
    std::getline(inFile, tempMake, ',');
    std::getline(inFile, tempModel, ',');
    std::getline(inFile, tempPropulsion, ',');
    std::getline(inFile, tempEngine, ',');
    std::getline(inFile, tempHp, ',');
    std::getline(inFile, tempColor, ',');
    std::getline(inFile, tempLength, ',');
    std::getline(inFile, tempBase_price, ',');
    std::getline(inFile, tempTotal_price, ',');
    
    cout << tempType << endl;
}