330 likes | 529 Views
Assignment Notes. Computer Streams. Initialize Variables!!! Month# / Ending Balance / Total Paid 1 / 809.665 / 2.36701e+263 2 / 617.03 / 2.36701e+263 3 / 422.068 / 2.36701e+263. Loops. Nested loops are expensive…. Streams & FileIO. Streams. A stream: Has a source
E N D
Computer Streams • Initialize Variables!!! Month# / Ending Balance / Total Paid 1 / 809.665 / 2.36701e+263 2 / 617.03 / 2.36701e+263 3 / 422.068 / 2.36701e+263
Loops • Nested loops are expensive…
Streams • A stream: • Has a source • Has a destination • Flows one way • Potentially infinite
Computer Streams • Stream (computer) : sequence of bytes from a source to a destination • Universal model for data transfer • Read a file • Stream from drive processor • Play a sound • Stream from memory sound card • Send a file over internet (TCP) • Stream from one network card other network card
Universal • Different streams, same concepts: • Input stream: program can read from • Use >> to pull datacin >> x; • Output stream: program can write to • Use << to push datacout << x;
FileStreams • <fstream> library defines file based streamsPrimary members: ifstream object that can read from a file ofstream object that can write from a file
Basic Output • Declare variable of type ofstream • Creates object • Tell ofstream object to open a file
Write To File • Use ofstream just like cout:
Where Does It Go? • File named MyData.txt in current directory • Running from command line: • Same directory as .exe • Inside QTCreator: • Project directory
Working Directory • Working Directory • Where a program "thinks" it is
Specifying a Location • Specify full path to place file in another location: • Use \\ instead of \ (Windows also allows /)
Finishing Up • Files will close themselves • Only close manually if you need to check .close() : close the file, flush the buffer .fail() : true if we had an error using file
Reading A File • Same recipe, with ifstream, use for input
Reading A File • Checking for issues with .fail()
Reading A Whole File • Read 5 numbers:
Detecting End • EOF : End Of File .eof() : the last read attempt exhausted the file • Hard to detect… have to run off the edge, then notice a problem
EOF Wonkiness • What is the difference between these files?
EOF Wonkiness • See it now?
EOF • Reading with >> causes EOF if no trailing whitespace • Advances past last char looking for more Ex: After reading in the 12 • myFile.eof() is true
EOF • With trailing whitespace (spaces or newlines) • Reads last number, sees newline, assumes more Ex: After reading in the 12 • myFile.eof() is false
Strategies • Check after input for failure:
Strategies • C++ idiom : use side effect of >> • Returns true if successful, false if failure
Mission • Average numbers and find highest value
Mission • Average numbers and find highest value
Get Highest/Lowest • Algorithm for find highest in list:highest = firstValuefor each other value { if value > highest highest = value}
Get Highest/Lowest • Alternative • Start with • Really low "high" value //pretty sure something is bigger…int highest = -99999; for each value { if value > highest highest = value}
Get Highest/Lowest • climits library defines highest/lowest possible values • INT_MAX/INT_MIN foolproof starting values: #include <climits> … int highest = INT_MIN; //everything is >= int lowest = INT_MAX; //everything is <=for each value { //do checks}
Mission • Average numbers and find highest value
Reading Records • Read in and report most pupular baby names rank boyname number girlname number
Start Small • Read in one record:
Read Multiple • Read all records in file: