Not able to read data from text file and convert it to integer
Hi,
I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:
20 40 20 25 30
20 30 40 20 25
20 40 20 25 30
20 30 40 20 25
30 40 40 30 40
i am using getline, and then converting it to int by using atoi. But my code is not working. can somebody help me out on this.
Code :
Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
int i,j,c;
int Ncoil, Ntank;
int **Tmin;
string **Tminstring;
char str[256];
cout << "Number of coils to be cleaned: ";
cin >> Ncoil;
cout << "Number of tanks: ";
cin >> Ntank;
Tmin = new int *[Ncoil];
for (int i = 0; i <= Ncoil; ++i)
{
Tmin[i] = new int[Ntank];
for(int j = 0; j <= Ntank; ++j)
{
ifstream myfile ("Tmin.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getline(myfile, line, ' ');
c = atoi(line);
Tmin[i][j] = c;
myfile.close();
}
}
else cout << "Unable to open output file";
}
}
return 0;
}