I'm trying to get the 2nd and 5th column details from the pipe separated file.
5th column is getting trimmed while reading from the dummy file. i tried using the get line function also. How to get the entire 5th column in the string?
File looks like:
1|6705|SW|447|C/A-"WAR" FROM CAR COMPANY |||RFD|E|0|
2|6706|CA|448|CAR TYPE OR CUST. ID, REQ |||RFD|E|0|
3|6707|CZ|448|CAR TYPE OR CUST. ID, REQ |||RFD|E|0|
The code
std::string cmd = "awk -F'|' '{ print $2, $5 }' 1.txt >> tmp.txt";
system(cmd.c_str());// extract the two columns and write to dummy file
ifstream read( "tmp.txt");
std::string line;
while (std::getline(read, line)) // Read the file line by line
{
std::istringstream iss(line);
string a, b;
if (!(iss >> a >> b)) { break; } // error
std::cout<<"a"<<a<<" b "<<b<<std::endl;
}
read.close();
system("rm tmp.txt");
The output
key(string): 6705, value(int): C/A-"WAR"
key(string): 6706, value(int): CAR
key(string): 6707, value(int): CAR
The std::cin >>
operator reads strings by blanks or newlines, so in your case, the value of C/A-"WAR" FROM CAR COMPANY
will be truncated into C/A-"WAR"
,FROM
, CAR
and COMPANY
. You may use getline
instead.
while (std::getline(read, line)) // Read the file line by line
{
std::istringstream iss(line);
string a, b;
iss>>a;
getline(iss,b);//This may work
std::cout<<"a"<<a<<" b "<<b<<std::endl;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments