【发布时间】:2014-01-18 12:13:36
【问题描述】:
我想创建延迟字幕的程序,当我运行下面的代码时,不是从第一行读取。 它就像文件中间一样。
#include "stdafx.h"
#include "iostream"
#include "cstdlib"
#include "fstream"
#include "string"
using namespace std;
int main(int argc, char *argv[]){
ifstream input; //input
char input_file[32]; //names of input and output
cout << "Enter name of input fille: "; ///user gives names of input
cin >> input_file;
input.open(input_file);
if (!input.good()){
cout << "File " << input_file << " dosen't exist." << endl;
return 1;
}
string row;
while (!input.eof()){
getline(input, row);
cout << row << endl;
}
system("pause");
return 0;
}
【问题讨论】:
-
为什么是
getline(input, row)?input>>row怎么了? -
顺便说一句,代码工作得很好。
-
@barakmanos
std::getline将读取包括空白在内的整行。input >> row默认不会 -
@P0W,感谢您的信息:)