Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
NEWLINE IN IOSTREAM
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Mike Wahler, post: 2533111"] #include <fstream> #include <istream> #include <iostream> #include <sstream> #include <string> std::istream& parse(std::istream& in) { std::string line; while(in) { while(in && line.empty()) { std::getline(in, line); } std::istringstream iss(line); unsigned int count(0); iss >> count; do { std::getline(in, line); } while(in && line.empty()); std::cout << "count == " << count << '\n'; for(unsigned int i(0); i < count; ++i) { while(in && !line.empty()) { std::cout << line << '\n'; std::getline(in, line); } std::cout << "[End of group " << i + 1 << "]\n"; while(in && line.empty()) std::getline(in, line); } } if(in.eof()) std::cout << "<End of Input>\n"; else std::cout << "** Error reading input**\n"; return in; } int main() { std::ifstream input("C:/test.txt"); if(input) parse(input); return 0; } /* ** (Not thoroughly tested ** */ Input: 2 1453 2123 3342 4234 5123 6511 6123 1234 1234 2345 3456 3456 7432 3 1453 2123 3342 4234 5123 6511 6123 1234 1234 2345 3456 3456 7432 1234 1234 2345 3456 3456 7432 Output: count == 2 1453 2123 3342 4234 5123 6511 6123 [End of group 1] 1234 1234 2345 3456 3456 7432 [End of group 2] count == 3 1453 2123 3342 4234 5123 6511 6123 [End of group 1] 1234 1234 2345 3456 3456 7432 [End of group 2] 1234 1234 2345 3456 3456 7432 [End of group 3] <End of Input> -Mike [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
NEWLINE IN IOSTREAM
Top