Read specific column in a line of text file

Hi everyone,
I have text file like this:
516457 +9989704016796.0 1
516471 +9989844018787.0 1
516481 +9989984013367.0 1
516484 +9990124023453.0 1
76516 +9990264014313.0 2
76522 +9990404025578.0 2
76541 +9990544016397.0 2
76561 +9990684018087.0 2
77081 +9990824015982.0 2

I want to read each column separately, below is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <sstream>
#include <typeinfo>
using namespace std;

int main()
{

int pos,pos2,pos3;
ifstream inf;

inf.open ("123.txt");
if (inf.is_open())
{

while (getline(inf,line))
{

if (line.find("Pkt_recv") != string::npos)
{
pos=line.find(word1);
pos3=line.find(word3);
line.replace(pos,word1.length(),"");
line.replace(pos3,word3.length(),"");
//inf >> a >> b >> c;
//cout << a << b << c << endl;

istringstream iss (line);
uint64_t column1;
uint32_t column3;
uint64_t column2;
iss >> column1 >> column2 >> column3;
cout << column1 <<" "<< column2 <<" "<< column3 << endl;

}

}
inf.close();
}
else cout << "Unable to open file inf \n";


return 0;
}
I could to get the values of column 1 and 2 and save in variables column1 and column2 but I could not get the third column value of text file. I do not know what is the problem. anyone can help me please. below is the output of mentioned code:

516688 9994744028138 0
516695 9994884028607 0
516709 9995024025201 0
516715 9995164029193 0
516735 9995304025456 0
516761 9995444028980 0
516767 9995584027224 0
516779 9995724028576 0

As you see the third column that I got is zeros, the values of third column should be either 1 or 2 or 3 or 4.
Regards
Last edited on
Your program doesn't compile.

Line 19: line is undefined.

Line 22: What does "Pkt_recv" have to do with any of the sample input you've shown?

Line 24-25: word1, word3 undefined.

column 2 is in double format. Suggest reading it into a double. Otherwise, you're going to be left with a '.' as the next character in the input buffer.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thanks for your reply
Yep, I changed the type into double_t and it is working now.
I am new member here and this is the first topic. I will use code tags in future.
Topic archived. No new replies allowed.