1 / 33

Assignment Notes

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

harva
Download Presentation

Assignment Notes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment Notes

  2. 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

  3. Loops • Nested loops are expensive…

  4. Streams & FileIO

  5. Streams • A stream: • Has a source • Has a destination • Flows one way • Potentially infinite

  6. 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

  7. 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;

  8. FileStreams • <fstream> library defines file based streamsPrimary members: ifstream object that can read from a file ofstream object that can write from a file

  9. Basic Output • Declare variable of type ofstream • Creates object • Tell ofstream object to open a file

  10. Write To File • Use ofstream just like cout:

  11. Where Does It Go? • File named MyData.txt in current directory • Running from command line: • Same directory as .exe • Inside QTCreator: • Project directory

  12. Working Directory • Working Directory • Where a program "thinks" it is

  13. Specifying a Location • Specify full path to place file in another location: • Use \\ instead of \ (Windows also allows /)

  14. 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

  15. Reading A File • Same recipe, with ifstream, use for input

  16. Reading A File • Checking for issues with .fail()

  17. Reading A Whole File • Read 5 numbers:

  18. 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

  19. EOF Wonkiness • What is the difference between these files?

  20. EOF Wonkiness • See it now?

  21. 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

  22. EOF • With trailing whitespace (spaces or newlines) • Reads last number, sees newline, assumes more Ex: After reading in the 12 • myFile.eof() is false

  23. Strategies • Check after input for failure:

  24. Strategies • C++ idiom : use side effect of >> • Returns true if successful, false if failure

  25. Mission • Average numbers and find highest value

  26. Mission • Average numbers and find highest value

  27. Get Highest/Lowest • Algorithm for find highest in list:highest = firstValuefor each other value { if value > highest highest = value}

  28. 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}

  29. 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}

  30. Mission • Average numbers and find highest value

  31. Reading Records • Read in and report most pupular baby names rank boyname number girlname number

  32. Start Small • Read in one record:

  33. Read Multiple • Read all records in file:

More Related