为什么我的程序读取的不仅仅是我的txt文件的第一行?
Why is my program reading more than just the first line of my txt file?
我在抓取textfile .txt的第一行时遇到了麻烦。我试过改变第二个参数中的数字并完全删除它。什么都没起作用,由于某种原因,我不能单独抓住1。
int main() {
ifstream fin;
char ex1[100];
fin.open("txtfile.txt");
if (fin.is_open()) {
cout << "YES FILE OPENED" << endl; //testing if file opened
}
while (fin.peek() != EOF){
fin.getline(ex1, 100, 'n');
cout << ex1 << endl;
}
}
txtfile.txt:低于
1
ABC
2
如果只需要第一行,在获得第一行后从while循环中中断:
while (fin.peek() != EOF){
fin.getline(ex1, 100, 'n');
cout << ex1 << endl;
break;
}
}
int main()
{
ifstream fin;
char ex1[100];
fin.open("txtfile.txt");
if (fin)
{
while (!fin.eof())
{
fin.getline(ex1, 100, 'n');
cout << ex1 << endl;
}
fin.close();
}
}
相关文章:
- 为什么如果我添加这一行,我的程序会不断询问值
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 为什么这一行不输出我的文本文件?
- 我的程序在另一行中读取名字然后是姓氏,而我需要它一起读取名字和姓氏
- 为什么我的程序在这一行崩溃
- 我想在矩阵的一行中添加标量
- 在Visual Studio中重建我的项目时,我会在同一行错误C2371上遇到2个错误
- 为什么我的代码只读取文件的最后一行
- 为什么我的程序写入.dat文件中的同一行
- 如何在C++的下一行打印我的名字
- 为什么我的重载<<运算符没有输出最后一行?
- 如何改进我的代码以正确计算唯一行数
- fstream正在为我的文件的每一行添加字符
- 如何使我的函数在某一行停止执行
- 希望我的代码逐行将矩阵元素转换为向量,它只将矩阵的最后一行显示为向量
- 我正在尝试递归地实现一个程序。我只是不明白为什么我的最后一行被执行了两次
- 为什么我的c++代码只从输入文件中读取一行?
- 为什么我的程序读取的不仅仅是我的txt文件的第一行?
- 从 txt 文件读取时,我的程序不会移动到下一行
- 写入文件时,只有最后一行保存到我的输出文件中