计算txt文本数据行数
ifstream file;//定义读取文件流
infile.open("data.txt");//打开文件
char c;
int line= 0;
while (file.get(c))
{
if (c == '\n')
line++;
}
file.close();//读取完成之后关闭文件
打开文本数据
#include <iostream>
#include <vector> //提供向量头文件
#include <algorithm> // 算法头文件,提供迭代器
#include <fstream> //提供文件头文件
#include <iomanip> //C++输出精度控制需要
using namespace std;
void readline(const char* strPath, int &line)
{
ifstream is(strPath);
char c;
while (is.get(c))
{
if (c == '\n')
line++;
}
is.close();
cout << "line=" << line << endl;
}
int main()
{
vector<double> V;
vector<double>::iterator it;
const char * filename = "data.txt";
ifstream file(filename);
double d;
while (file >> d)
{
V.push_back(d);//将数据压入堆栈
}
file.close();
int line = 1;
readline(filename, line);
double aa[100][2];
for (it = V.begin(); it != V.end(); )
{
for (int i = 0; i < line; i++)//定义行循环
{
for (int j = 0; j < 2; j++)//定义列循环
{
aa[i][j] = *it;
it++;
}
}break;
}
cout << aa[0][0] << " " << aa[0][1] << setprecision(16) << endl;//检验
cout << aa[1][0] << " " << aa[1][1] << setprecision(16) << endl;
cout << aa[2][0] << " " << aa[2][1] << setprecision(16) << endl;
cout << aa[3][0] << " " << aa[3][1] << setprecision(16) << endl;
cout << aa[4][0] << " " << aa[4][1] << setprecision(16) << endl;
cout << aa[5][0] << " " << aa[5][1] << setprecision(16) << endl;
cout << aa[6][0] << " " << aa[6][1] << setprecision(16) << endl;
return 0;
}
结果
参考:https://blog.csdn.net/m0_38036750/article/details/83478602