Reading input from text file to array in c++

前端 未结 2 1882
星月不相逢
星月不相逢 2021-01-29 09:36

Alright, be gentle, since I\'m very much a novice to programming. So far I\'ve only studied C++ and I\'m running Visual Studio 2010 as my compiler. For this program, I\'m trying

2条回答
  •  春和景丽
    2021-01-29 09:43

    fstream's getline function has these signatures.

    std::istream& getline (char* s, streamsize n );
    std::istream& getline (char* s, streamsize n, char delim );
    

    In your code...

    string nameAr[EMP_NUM];
    // stuff...
    inFile.getline(nameAr[i], EMP_NUM, "*");
    

    You are trying to use a std::string where getline wants a char* - you cannot convert from string to a char* like this.

    One approach is you can use a character buffer to store the contents of getline, like this.

    const int BUFFER_SIZE = 1024;
    char buffer[BUFFER_SIZE];
    inFile.getline(buffer, BUFFER_SIZE, '*');
    // more stuff...
    

    A better solution

    I'd recommend using std::getline from , as you can use string types instead of C-strings.

    #include 
    
    // stuff...
    std::getline(inFile, nameAr[i], '*');
    

    edit Links for you since you are learning

    • std::getline function example
    • std::getline function description

提交回复
热议问题