.txt file to 2d array - getline()

Hi,

I managed to import data from a single dimension array and , after much hassle worked out well. However now I need to do the same with a 2d array, and AGAIN I already spent two days trying to figure out a solution.

What I have for now is the array in a txt file "rates.txt".

2;5
3;7
5;10
7;20
14;20
13;12

So the delimiter is the ";", there are 2 rows and 6 lines. So the 2d array size is defined as follows:

string array[6][2]

Now, the code I am trying to work out:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;  

int main () {
int i=0,j=0;
string line;
string array[6][2];

    ifstream ratesfile("C:\\rates.txt");   
    if (ratesfile.is_open()) printf("File Open\n"); else printf("File not open\n");

    cin.get();

 
     while (! ratesfile.eof()){      
     getline(ratesfile,line,';');
     array[i][j]=line;
     cout << array[i][0]<< endl;  //<-- All results are stored here
     cout << array[i][1]<< endl;  //<-- There are no results stored here :(
     i++;}      

    cin.get();

	return 0;
} 

Please help,

Thx
Last edited on
In your code j never changes.

You could use two nested loops for this. One for each array dimension:

1
2
3
4
5
6
7
for(int i = 0; i < 6; ++i) // go through each line
{
    for(int j = 0; j < 2; ++j) // go through each item
    {
        // read item into array[i][j]
    }
}
Last edited on
Also, don't forget to close your file.
Well, thx for the advice, I have modified the code accordingly, getting near something, but still a bit confused.

Here's the new code below, and in // my questions:

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
#include <curses.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;  

int main () {
int i=0,j=0;
string line;
string array[6][2];

ifstream ratesfile("C:\\Documents and Settings\\wboustany.SOCO\\Bureau\\analyser\\rates.txt");   
if (ratesfile.is_open()) printf("File Open\n"); else printf("File not open\n");
 
     while (! ratesfile.eof()) {  
      for(int i = 0; i < 5; ++i) {
       for(int j = 0; j < 1; ++j) {   
        getline(ratesfile,line,';');
        array[i][j]=line;
        cout << array[i][j]<< endl; }}} // works ok although it prints the last number of the array several times (the number 12).
        
cin.get();
     
     for(int i = 0; i < 6; ++i)
      {     
        cout << array[i][0]<< endl;
        cout << array[i][1]<< endl;  }      //Doesn't give the requested data, why ?
        
cin.get();
        
      for(int i = 0; i < 6; ++i) {
       for(int j = 0; j < 2; ++j) {     
        cout << array[i][j]<< endl;  }}    //Doesn't give the requested data, why ?
                 
ratesfile.close();    
     
     cin.get();  
      
	return 0;
}


So basically, when I request data from an array outside the "while" loop, I dont get the data I am expecting,

I dont understad why.
Last edited on
1
2
for(int i = 0; i < 5; ++i) {
       for(int j = 0; j < 1; ++j)


Change it for:

1
2
for(int i = 0; i < 6; ++i) {
       for(int j = 0; j < 2; ++j)



In addition, u told that there's 6 rows and 2 columns in your file, so you don't have to use while anymore. It's good when you don't know how long file is.

In the end, you declared two variables 'i' and 'j' in the beginning. You don't have to make new in for loops. All you've got to do is:

for(i=0; i<6; ++i)

Same for 'j'.
Your input loop was going from 0 to 4 for i and j was always at 0.

To go from 0 to 5 you have to do for(int i = 0; i < 6; ++i). You made i < 5 which only goes up to 4.

Also your while() loop doesn't do anything.

Also you need to check the results of the input. So I put in a check on the return value of std::getline():

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
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	int i = 0, j = 0;
	string line;
	string array[6][2];

	ifstream ratesfile("input.txt");
	if(ratesfile.is_open())
//		printf("File Open\n"); // avoid printf()
		std::cout << "File Open\n"; // use std::cout
	else
//		printf("File not open\n");
		std::cout << "File not open\n"; // use std::cout

	for(int i = 0; i < 6; ++i) // should go from 0 - 5
	{
		for(int j = 0; j < 2; ++j) // should go from 0 - 1
		{
			// The return from getline() should be
			// checked to make sure the read was valid
			if(getline(ratesfile, line, ';'))
			{
				array[i][j] = line;
				cout << array[i][j] << endl;
			}
		}
	}

	cin.get();

	for(int i = 0; i < 6; ++i)
	{
		cout << array[i][0] << endl;
		cout << array[i][1] << endl;
	} //Doesn't give the requested data, why ?

	cin.get();

	for(int i = 0; i < 6; ++i)
	{
		for(int j = 0; j < 2; ++j)
		{
			cout << array[i][j] << endl;
		}
	} //Doesn't give the requested data, why ?

	ratesfile.close();

	cin.get();

	return 0;
}
Last edited on
Thank you very much for your answers guy, just a last question to see if I am getting this thing right.

The following code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for(int i = 0; i < 6; ++i)
	{
		cout << array[i][0] << endl;
		cout << array[i][1] << endl;
	} //Doesn't give the requested data, why ?

	cin.get();

	for(int i = 0; i < 6; ++i)
	{
		for(int j = 0; j < 2; ++j)
		{
			cout << array[i][j] << endl;
		}
	} //Doesn't give the requested data, why ? 


...still done give the expected results. I wondered if initially I formatted my text file wrongly, so I changed it from this:

2;a
3;b
5;c
7;d
14;e
13;f

to this:

2;a;
3;b;
5;c;
7;d;
14;e;
13;f;

and it worked out well. Am i getting it right ?

Thank you all very much for your help.
Yes, you're right. getline() function extracts characters from 'ratesfile' to 'line' until it find delimitation character which is ';' in this case. Also it stops if the end of file is reached. Default delimitation character is newline character ('\n').
Its all sorted then,

Thanks again and regards,

Wissam
Topic archived. No new replies allowed.