Reading data from .txt file

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 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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;
 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;
}



Last edited on
what exactly is the problem ?

Edit: please put your code in code tags.
Last edited on
"c = atoi(line);" is creating error, i don't know why.
Also, I have created a breakpoint before that line too but "line" didn't show any value. can u please run on your compiler.

Thanks
sorry, i didn't know about code tags. now i changed.
atoi takes a c_string pointer.

c = atoi(line.c_str());
Thanks Sharmaji,
It worked. I am just a beginner in c++.
Topic archived. No new replies allowed.