Reading from Text File to Array

Lets say my text file looks like this.

POS/DATE 1 2 3
A 40 11 16
B 30 12 17
C 20 13 18
D 10 14 19
E 90 15 20

There are 6 ROWS and 4 COLUMNS. Please note that "POS/DATE" is in the "0" index.

When i read the dates (1,2,3....) how do i NOT readin the "POS/DATE" on to the array.

Similarly, how do i not read in "POS/DATE" when i create an array for the pos. (A,B,C,D.....)??

Please help.

Here's my basic 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
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <array>
#include <sstream>	
#include <string>

using namespace std;

int main()
{

	ifstream inFile;
	ofstream outFile;

	const int datesSize = 30;
	string datesArray[datesSize];

	const int posSize = 13;
	string posArray[posSize];

	inFile.open("data.txt");

		if (!inFile)
			cout << "Error Opening File\n";

		else
		{
			for (int i = 0; i < datesSize; i++)
			{
				
				inFile >> datesArray[i];
				
				cout << " " << datesArray[i];
				
			}

			for (int i = 0; i < posSize; i++)
			{
			        inFile >> posArray[i];
				cout << " " << posArray[i];
			}


			inFile.close();
		}

		cout << endl;
	system("pause");

	return 0;

}
Last edited on
Simply create a temporary variable and read it to this variable before you start feeding the array.
Your Program is working fine
@coder777

How do i do that?

@bird1234

It works, but the array is reading, "POS/DATE".
I want to NOT read that.
@EdwardMiller91

I'm new to coding, have no idea what he said. I tried coding and i'm stuck at above code. F$%^... :(
If you want to skip the whole of the first line
POS/DATE 1 2 3
then use ignore:
 
    inFile.ignore(1000, '\n');

Or if you want to skip only the first part of the line, "POS/DATE", you could either do something like this:
1
2
    string dummy;
    inFile >> dummy;
as suggested by coder777.

It's not entirely clear what data you want to be read into the array datesArray and what data should go into the array posArray, so it's hard to go much further, except to say that the code looks problematic in that it doesn't check for errors (such as end of data) when reading from the file.
Here is what i have so far.

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
58
59
60
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <array>
#include <sstream>	
#include <string>

using namespace std;

int main()
{

	ifstream inFile;
	ofstream outFile;

	const int datesSize = 50;
	string datesArray[datesSize];

	const int posSize = 13;
	string posArray[posSize];
	string dummy;

	inFile.open("data.txt");

	if (!inFile)
		cout << "Error Opening File\n";

	else
	{
		for (int i = 0; i < datesSize; i++)
		{

			inFile >> datesArray[i];

			cout << " " << datesArray[i];
			
			
		}

		

		for (int j = 0; j < posSize; j++)
		{
			inFile >> dummy;
			inFile >> posArray[j];
			cout << " " << posArray[j];
		}


		inFile.close();
	}

	cout << endl;
	system("pause");

	return 0;

}


And still reading "POS/DATE"

Last edited on
You have
 
    inFile >> dummy;
at line 46

But by that time there have already been many items read from the file at line 35, so it is too late by then
 
    inFile >> datesArray[i];


Line 35 is in a loop which attempts to read 50 items from the file. But there are only 24 items altogether?

@Chervil, i finally got it. What @ coder777 and you told worked. Read it to a temporary string and ignore it.

Thank you.
Topic archived. No new replies allowed.