How to read in a text file and store into an array?

HI everyone,

I am trying read in a text file with a 10 by 2 array of values into my program and I would require the program to assign it back into another array of 10 by 2. The columns are separated by one tab t.

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

As the input is coming from a text file, I would have to convert it into a string and the point where I am stuck is converting the string into an array.

The problem I am having is that when the input file is converted into a string, the tab or whitespace is ignored and the string becomes "11" for the first line.

is there anyway that I can split up the "11" and store the first "1" into array[0][0] and second "1" into array[0][1] and so on...? this array will then be used to perform further calculations.

Below is the code I have written so far. Could anyone kindly please provide a solution for this problem?

Thank you very much

Jawsh

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define ArrayRow 10
#define ArrayCol 2

using namespace std;

int main ()
{
	int Row;
	int Col;
	string StringValues;
	double Results [ArrayRow][ArrayCol];

	ifstream inputfile ("Sample - Input Array 2 Columns.txt");	

	if (inputfile.is_open())
	{
		for(Row = 0; Row < ArrayRow; Row++)
		{
			getline (inputfile, StringValues);
			istringstream convert1(StringValues);
		
			for(Col = 0; Col < ArrayCol; Col++)
			{
				inputfile >> Results[ArrayRow][ArrayCol];
				cout << "Results[" << Row << "][" << Col << "]: ";
				cout << Results[Row][Col] << endl; //printing this out just to check the array created is correct.
			}
		}
	}
	else 
		cout << "Unable to open file";
	
	return 0;
}
Use the >> operator instead of getline - it stops at whitespaces. If you only want the input to stop at tabs and not at other space characters, use getline and provide the third argument as 't' (the third argument of getline is the delimiter character, it's 'n' by default).

EDIT: Anyone know why you have to escape backslashes in this forums?
Last edited on
Thank for the suggestion.

I have tried using the delim slash t as the third argument and manage to extract the value before the tab which is just "1". May I know how do I go about extracting the 2nd "1" from the next column before proceeding to the next row???

Thanks

Jawsh
Uh yeah, just call getline again just this time without the '\t'
Thank you hanst99.

I have came up with the code below and the output is exactly what I wanted but it seems to be skipping the first number in the front. I have debug it and watch the variables change but I still do not understand why it skips the first number...

Anyone have any idea why is it skipping the first number??

The output for the below code is:

Results[0][0]: 1
Results[0][1]: 2 //this output should still be 1
Results[1][0]: 2
Results[1][1]: 3
Results[2][0]: 3
Results[2][1]: 4
Results[3][0]: 4

and so on...

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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define ArrayRow 10
#define ArrayCol 2

using namespace std;

int main ()
{
	int Row;
	int Col;
	int ColumnCounter = 1;
	string StringValues;
	double Results[ArrayRow][ArrayCol];

	ifstream inputfile ("Sample - Input Array 2 Columns.txt");	

	if (inputfile.is_open())
	{
		for (Row = 0; Row < ArrayRow; Row++)
		{
			for (Col = 0; Col < ArrayCol; Col++)
			{
				if (ColumnCounter == 1)
				{
					getline (inputfile, StringValues, 't');
					istringstream convert1(StringValues);	

					inputfile >> Results[Row][Col];
					cout << "Results[" << Row << "][" << Col << "]: ";
					cout << Results[Row][Col] << endl;
					ColumnCounter = 2;
				}
				else
				{
					getline (inputfile, StringValues);
					istringstream convert1(StringValues);	

					inputfile >> Results[Row][Col];
					cout << "Results[" << Row << "][" << Col << "]: ";
					cout << Results[Row][Col] << endl;
					ColumnCounter = 1;
				}
			}
		}
	}
	else 
		cout << "Unable to open file";
	
	return 0;
}
Topic archived. No new replies allowed.