Reading specific data from a file

I have a text file (say "data.txt") that contains some data in different columns. Columns are separated by tab. I want to read numeric data from 3rd column (or any other specific column) into an array.
The file content is like this:

id temp activity
1 10 100
2 20 150
3 25 170
....
.....

Can somebody help?

This file is generated by another program. So, I can modify it (e.g. using space or comma instead of tab), if that would help in reading data.
Thanks in advance.
God bless!!!
@kumard

Check here,
http://cplusplus.com/forum/beginner/53656/
under my answer, for an idea how to get specific data from certain columns, etc. This should point you in the right direction.
@whitenite1

Thanks for reply.
I got a good idea about reading data from file. However my problem is reading only one column as my actual data file will be very long (more than 10000 lines). I will try to explain what I am looking for:

Say my file is "data.txt" and content is:
1 10 100
2 20 150
3 25 170
4 30 180
5 40 200
6 50 225
7 60 250
8 65 300
9 70 350
10 80 500

I wrote a program that can store this data into 3 arrays (one for each column):
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include<conio.h>
using namespace std;

int main() {
    int x, y, z, i;
    int a[15], b[15], c[15];//size of array more than number of entries in data file
    ifstream infile;
    infile.open("data.txt");//open the text file
    if (!infile) {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }
    i=0;
    while (!infile.eof())
    {
    //To make three arrays for each column (a for 1st column, b for 2nd....)
    infile>>x>>y>>z;
    a[i]=x;
    b[i]=y;
    c[i]=z;
    i++;
    }  
    cout<<a[0]<<"\t"<< b[0]<<"\t"<<c[0]<<endl; // To print 1st entry (1st row), similarly we can print any row
    infile.close();
    getch();
}


My queries are:
1. if I have headings of data (say "id" on 1st column, "temperature" on 2nd column......) in the 1st line of file, then this code does not work. Is there a way that I can skip 1st line or ignore reading of characters in the file
2. As I will have very long data file, is there any way that I can only read and store 2nd column, without reading 1st and 3rd column.

Can anybody help in this?
Thanks in advance.



Can anybody help in this?
Thanks in advance.
@kumard

Try this.
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
61
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include<conio.h>

using namespace std;

int main()
{
    int x, row , i;
	int a[200];
	int b[200];
	int c[200];//size of array more than number of entries in data file
    char choice = ' ';
	ifstream infile;
    infile.open("data.txt");//open the text file
    if (!infile)
	{
       cout << "Unable to open file";
       exit(1); // terminate with error
    }
    i=0;

    while (!infile.eof())
    {
    //To make three arrays for each column (a for 1st column, b for 2nd....)
    infile >>a[i] >> b[i] >> c[i];
    i++;
    }
	i--;


	cout << "Which Id #, should I show?" << endl;
	row = 0;
	do
	{
		cout << endl << "Row ( 1 to " << i << " ) > ";
		cin >> row;
	} while ( row < 1 && row > i);

	
	do
	{
		cout << "Show the (L)ow temp or (H)i or (B)oth? ";
		cin  >> choice;

	} while ( choice !='L' && choice != 'l' && choice != 'H' && choice != 'h' && choice !='B' && choice != 'b');

	if ( choice =='H' || choice == 'h' )
		cout << "ID# " << a[row-1] << " = High temp of " << b[row-1] << "..." << endl << endl;
	else if ( choice =='L' || choice == 'l' )
		cout << "ID# " << a[row-1] << " = Low temp of " << c[row-1] << "..." << endl << endl;
	else if ( choice =='B' || choice == 'b' )
		cout << "ID# " << a[row-1] << " = High temp of " << b[row-1] << " and the low of " << c[row-1] << endl << endl;

for (x=0;x<i;x++)
    cout<<a[x]<<"\t"<< b[x] << "\t" <<  c[x] <<endl; // To print all the entries.
    infile.close();
    _getch();
}
Last edited on
Topic archived. No new replies allowed.