有谁知道为什么我的代码没有从第一行读取文件
Does anyone know why is my code not reading file from first line
我想创建用于延迟字幕的程序,当我运行下面的代码时,没有从第一行读取。它像文件的中间一样分层。
#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;
}
首先,eof()
不应该放在while()
中,有关详细信息,请参阅此处。所以改变
while (!input.eof()){
getline(input, row);
cout << row << endl;
}
自
while (getline(input, row)){
cout << row << endl;
}
看看进展如何。从文件中间阅读真的很奇怪。也许您可以进一步将文件的内容分享给我们,以更好地解决问题。
您仍然可以在读取任何内容之前使用 input.seekg(0, input.beg)
手动将位置设置为开头。
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使代码打印文本文件中的第一行?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将字符串存储到文件下一行的变量中
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 比较两个没有一行共同点的大文件
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 到达分隔符时跳到文本文件的下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文本文件中随机选取一行
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- 从文件中的一行读取特定数据-C++
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 为什么这一行不输出我的文本文件?
- 从文件读取并在一行中写入 cout
- 如何有效地读取C 中的最后一行文件
- 将一行文件读入对象中的两个变量