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:
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!!!
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):
#include <iostream>
#include <iomanip>
#include <fstream>
#include<conio.h>
usingnamespace 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.