Lec19StringStreams_6per.pdf - EECS 402 Motivation The...

Info icon This preview shows pages 1–2. Sign up to view the full content.

1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 19 Savitch N/A String Streams EECS 402 EECS 402 Andrew M Morgan 2 Motivation Write a program that writes out a different file each iteration of a loop, using the loop counter in the filename for (i = 1; i < 3; i++) { if (i == 1) { fname = "out1.txt"; } if (i == 2) { fname = "out2.txt"; } outFile.open(fname); //..write to file outFile.close(); } This is ok for a loop of 2 iterations. How to do this for a loop of 100 or more iterations? fname = "out" + i + ".txt"; //Will NOT work. operator+ is for appending one string onto the end of another string Would like to "print" strings, integers, doubles, etc. to a string variable EECS 402 EECS 402 Andrew M Morgan 3 Recall Stream Definition A general definition of a stream A sequence of bytes, interpreted in groups of appropriate size, that can be used as input or output Input stream: Such a sequence of bytes, used as input, to set memory locations to appropriately typed values Output stream: Such a sequence of bytes, used as output, to screen, data file, etc. Currently known streams Input: cin (keyboard), ifstream (input data file stream) Output: cout (keyboard), ofstream (output data file stream) EECS 402 EECS 402 Andrew M Morgan 4 Another Type Of Stream A general definition of a string A sequence of characters Characters are stored in one byte of data each, so really, this definition could also read "a sequence of bytes" Same definition as a stream from previous page. Strings can be used as input or output streams istringstream: Data type that will use a string as an input stream
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern