Reputation: 3774
I am not able to get the expected output for below function.
in.txt file
-rw-r--r-- 1 air staff StructEx.cpp
-rw-r--r-- 1 air staff Struct2ex.cpp
drwxr-xr-x 3 air staff app.dSYM
-rw-r--r-- 1 air staff MyStruct.cpp
excepted - to print column 1 and column 4 contents from the above in.txt file. some thing like below
-rw-r--r--StructEx.cpp
-rw-r--r--Struct2ex.cpp
drwxr-xr-xapp.dSYM
-rw-r--r--MyStruct.cpp
actual output -
-rw-r--r--tructEx.cpp
-rw-r--r--tructEx.cpp
-rw-r--r--tructEx.cpp
-rw-r--r--tructEx.cpp
the above output is wrong, what I am missing in below string stream function.
void readingFromFile2(){
ifstream inputFile("in.txt");
string line;
stringstream entireLine;
char s = ' ';
string p1, p2, p3, p4, p5;
while(!inputFile.fail()){
getline(inputFile, line);
entireLine << line;
entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5 ;
cout << p1 << p4 << endl ;
}
}
edit from important comments, I think below function makes more sense, but still the same out put
void readingFromFile2(){
ifstream inputFile("in.txt");
string line;
stringstream entireLine;
char s = ' ';
string p1, p2, p3, p4, p5;
char n ='\n';
while(getline(inputFile, line)){
entireLine << line;
entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ;
cout << p1 << p4 << endl ;
}
}
Update - entire code
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void readingFromFile2();
int main(){
readingFromFile2();
}
void readingFromFile2(){
ifstream inputFile("in.txt");
string line;
stringstream entireLine;
string p1, p2, p3, p4, p5;
while(getline(inputFile, line)){
entireLine << line;
entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ;
cout << p1 << p2 << p3 << p3 << p4 << p5 << endl ;
}
}
Now it is working,
use stringstream entireLine(line);
instead of entireLine << line;
Upvotes: 1
Views: 90
Reputation: 6407
While using >>
for stream input you should use
entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ;
because spaces (' '
, \t
and \n
) are ignored.
UPDATE:
Why "-rw-r--r-- 1 air staff StructEx.cpp" gives tructEx.cpp
instead of staff
when entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5 ;
is used?
Let's look step by step:
1) entireLine >> p1
string till the first space is read to p1
: -rw-r--r--
goes to p1
2) entireLine >> s
skips the space and 1
goes to s
3) entireLine >> p2
skips the space and air
goes to p2
4) entireLine >> s
skips the space and s
(beginning of staff
) goes to s
5) entireLine >> p3
just reads taff
to p3
6) entireLine >> s
skips the space and S
(beginning of StructEx.cpp
) goes to s
7) entireLine >> p4
eventually we have value for p4
and this is tructEx.cpp
, because S
was taken at step 6) to s
UPDATE 2
Simplified demo:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line = "-rw-r--r-- 1 air staff StructEx.cpp";
stringstream entireLine;
char s = ' '; // not required
string p1, p2, p3, p4, p5;
char n = '\n'; // not required
entireLine << line;
entireLine >> p1 >> p2 >> p3 >> p4 >> p5;
cout << p1 << ", " << p2 << ", " << p3 << ", " << p4 << ", " << p5 << endl;
return 0;
}
gives
But
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string line = "-rw-r--r-- 1 air staff StructEx.cpp";
stringstream entireLine;
char s = ' '; // initialization has no sense
string p1, p2, p3, p4, p5;
char n = '\n'; // not required
entireLine << line;
entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5; // HERE!!!
cout << p1 << ", " << p2 << ", " << p3 << ", " << p4 << ", " << p5 << endl;
return 0;
}
produces
FINAL UPDATE
void readingFromFile2(){
ifstream inputFile("in.txt");
string line;
//stringstream entireLine;
string p1, p2, p3, p4, p5;
char str[51];
while (getline(inputFile, line)){
stringstream entireLine(line); //entireLine << line;
entireLine >> p1 >> p2 >> p3 >> p4 >> p5;
cout << p1 << p4 << endl;
}
}
Upvotes: 4