| | |
Doing something stupid with stringstream
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
I put together this little example to test my sanity, and it failed!
The output is
Can anyone spot the stupid bug?
Thanks,
Dave
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; /* Example.txt 23 test 4.5 */ int main(int argc, char *argv[]) { string Filename = argv[1]; cout << "Filename: " << Filename << endl; int Integer; string String; double Double; ifstream fin(Filename.c_str()); string line; stringstream linestream; getline(fin, line); linestream.str(""); linestream << line; linestream >> Integer; getline(fin, line); linestream.str(""); linestream << line; linestream >> String; getline(fin, line); linestream.str(""); linestream << line; linestream >> Double; fin.close(); cout << "Integer: " << Integer << endl; cout << "String: " << String << endl; cout << "Double: " << Double << endl; return 0; }
The output is
C++ Syntax (Toggle Plain Text)
Filename: Example.txt Integer: 65535 String: Double: 4.86439e-270
Can anyone spot the stupid bug?
Thanks,
Dave
A stringstream is still a stream. When you read to end-of-file, you need to clear the state before you can do anything substantial with the stream again:
C++ Syntax (Toggle Plain Text)
linestream.clear(); linestream << line; ...
I'm here to prove you wrong.
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
That's what I thought I was doing with
But I changed all of those to
And I get the same garbage results.
Dave
C++ Syntax (Toggle Plain Text)
linestream.str("");
But I changed all of those to
C++ Syntax (Toggle Plain Text)
linestream.clear()
And I get the same garbage results.
Dave
I get correct results with the following code. The only changes were the ones you claim to have made:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; /* Example.txt 23 test 4.5 */ int main(int argc, char *argv[]) { string Filename = argv[1]; cout << "Filename: " << Filename << endl; int Integer; string String; double Double; ifstream fin(Filename.c_str()); string line; stringstream linestream; getline(fin, line); linestream << line; linestream >> Integer; getline(fin, line); linestream.clear(); linestream << line; linestream >> String; getline(fin, line); linestream.clear(); linestream << line; linestream >> Double; fin.close(); cout << "Integer: " << Integer << endl; cout << "String: " << String << endl; cout << "Double: " << Double << endl; return 0; }
I'm here to prove you wrong.
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
haha OH NO! The file was in the wrong directory... it works now.
I didn't gaurd it with
because it was just a toy example... but clearly I should have!
Sorry for the silly mistake.
Dave
I didn't gaurd it with
C++ Syntax (Toggle Plain Text)
if(fin == NULL)
because it was just a toy example... but clearly I should have!
Sorry for the silly mistake.
Dave
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: how can a "scanf" input be a character when printed?
- Next Thread: write file in Linux using Multithreading
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






