【发布时间】:2023-03-23 04:25:01
【问题描述】:
我正在尝试获取 txt 文件中的行数。这是我一直在尝试实现的方法,但它会打印 n=1。
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
int n=0;
string line;
ifstream fdatos;
fdatos.open("temp_toma5.txt");
while (!fdatos.eof()){
getline(fdatos, line);
n++;
}
cout<<"n = "<<n<<endl<<endl;
fdatos.close();
return 0;
}
这是文件的结构
0 0.03 0
5 0.03 0.01
10 0.02 0
15 0.02 0
20 0.02 0
25 0.03 0
30 0.03 0
在此感谢您
【问题讨论】:
-
请阅读 SOtour,阅读How to Ask,以及this question checklist。然后edit你的问题来改进它。比如告诉我们会发生什么,应该发生什么。
-
“它不起作用,我不知道为什么” 没有描述特定行为或提出问题。充其量只是一种抱怨。这可能意味着几乎任何事情。你的代码甚至可以编译吗?它是否成功打开文件?它输出的值是否与您的预期不同?具体一点。