Stringstreams & Parsing

1

CS 103 Unit 14 ? Stringstreams and Parsing

2

I/O Streams

? '>>' operator used to read data from an input stream

? Always stops at whitespace

? '> buf;

fp

File text T h e e n d . \n EOF

EOF BAD FAIL

buf T h e \0

000

inf >> buf;

fp

File text T h e e n d . \n EOF

buf e n d . \0

EOF BAD FAIL 000

inf >> buf;

fp

File text T h e e n d . \n EOF

buf e n d . \0

EOF BAD FAIL 101

5

Which Option Works?

#include #include using namespace std; int main() {

vector nums; ifstream ifile("data.txt"); int x; while( !ifile.fail() ){

ifile >> x; nums.push_back(x); } ... }

data.txt

7 8 EOF

nums

_ ___

#include #include using namespace std; int main() {

vector nums; ifstream ifile("data.txt"); int x; while( 1 ){

ifile >> x; if(ifile.fail()) break; nums.push_back(x); } ... }

Goal is to read all integers from the file into a vector.

Which of the 3 works?

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download