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

问题描述

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

浮船,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,tempModel,tempPropulsion,tempEngine,temPHP,tempColor,tempLength,tempBase_price,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,');
    ^~~~~~~
/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 char* 流中读取一行作为 FILE* 字符串。您使用的是 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,');
    inFile.getline(tempEngine,');
    inFile.getline(tempHp,');
    inFile.getline(tempColor,25,');
    inFile.getline(tempLength,');
    inFile.getline(tempBase_price,');
    inFile.getline(tempTotal_price,');
    
    cout << tempType << endl;
}

虽然 std::getline() 会更好,特别是如果您要将字符串传递给 std::stoi()std::stod(),它们接受 std::string 输入,而不是 {{1} } 输入:

char[]