devo leggere un file in cui in ogni riga ci sono 2 numeri e restituire il numero di righe del file. Potete spiegarmi come fare?
Ecco il mio tentativo
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdlib.h>
#include <string>
#define NMAX 1000000
using namespace std;
//FUNCTION PROTOTYPE
int ReadXYData (string file_name, vector <double> &x, vector <double> &y);
main()
{
int result;
string file_name;
vector<double> x(NMAX), y(NMAX);
cout << "This program reads a tabulated function from a file and returns ";
cout << "the total number of lines read in.\n\n";
while (1)
{
cout << "Insert the name of the file to be read: ";
cin >> file_name;
result=ReadXYData (file_name, x, y);
if (result==-1)
cout << "Error! The file could not exist or be unreadable\n" << endl;
else if (result==0)
cout << "The file is empty" << endl;
else if (result==NMAX)
cout << "The file contains more then " << NMAX << "lines" << endl;
else
cout << "The file contains " << result << " lines\n" << endl;
}
system ("pause");
}
// read a file
int ReadXYData (string file_name, vector <double> &x, vector <double> &y)
{
int lines=-1, i=0;
ifstream file; // create file
file.open (file_name.c_str()); // open file
if (file.is_open()) //Has been it opened?
{
lines=0;
while (! file.eof() || i<=NMAX)
{
file >> x[i];
file >> y[i]; // reading
cout << x[i] << endl;
i++;
lines=i;
}
file.close (); //close the file
}
return lines;
}