我对文件输入似乎有一个基本的误解。我假设我的方法适用于我正在从事的项目,但事实并非如此。这是代码:

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

int main(){
   ifstream input;
   char fname[20], lname[20];
   input.open("text.txt");

   input.getline(lname, 20, ',');
   input.getline(fname, 20, ' ');
   cout << lname;
   cout << fname;

}


从文件中我有:

Squarepants, Spongebob


并且cout语句不输出任何内容

我做错了什么?

谢谢

最佳答案

这可能是一个很好的使用模式:

std::string lineOfText;

while(fileAccessor.good()) {
    fileAccessor.getline(lineOfText);
    //
    // do stuff
    // do stuff
    //
    if(fileAccessor.eof()) break;
}

关于c++ - 使用getline从文件读取输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29481308/

10-11 22:52
查看更多