<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Streams :: CS3 course</title><link>https://robark.gitlab.io/streams/index.html</link><description>Streams File I/O, stringstreams</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 01 Jun 2021 22:47:18 +0000</lastBuildDate><atom:link href="https://robark.gitlab.io/streams/index.xml" rel="self" type="application/rss+xml"/><item><title>Fileio</title><link>https://robark.gitlab.io/streams/fileio/index.html</link><pubDate>Thu, 27 May 2021 22:52:27 +0000</pubDate><guid>https://robark.gitlab.io/streams/fileio/index.html</guid><description>File I/O Various ways to read from a file
#include &lt;iostream&gt; #include &lt;fstream&gt; using namespace std; // dumps file to screen int main() { string line,word; ifstream f("mydata.txt"); //ifstream f; //f.open("mydata.txt", ios::in); cout&lt;&lt;"----------reading by line -----------------------"&lt;&lt;endl; //ex #1 while ( getline(f,line) ) //line delimeted { cout&lt;&lt;line&lt;&lt;endl; } //f.close(); //f.open("mydata.txt", ios::in); f.clear(); // clear eof state flag from true to false //f.seekg(0); // set seek get file pointer to beginning of file //f.seekg(0,ios::beg); f.seekg(0,f.beg); //resets file pointer back to the top cout&lt;&lt;"----------reading by words-----------------------"&lt;&lt;endl; //ex #2 while ( f &gt;&gt; word ) //space delimited { cout&lt;&lt;word&lt;&lt;endl; } f.clear(); f.seekg(0,ios::beg); //set seek get file pointer with offset, so 0 offset from beginning of file cout&lt;&lt;"-----------reading by line but using eof() or good()----------------------"&lt;&lt;endl; //ex #3 //while ( !f.eof() ) while ( f.good() ) { getline(f,line); //use with !f.eof() cout&lt;&lt;line&lt;&lt;endl; } cout&lt;&lt;"-----------reading by character-----------------------"&lt;&lt;endl; f.clear(); f.seekg(0,ios::beg); //set seek get file pointer with offset, so 0 offset from beginning of file //ex #4 char c; while ( f.get(c) ) { cout&lt;&lt;c; } f.close(); return 0; } Reading and Writing from a file</description></item><item><title>Structs</title><link>https://robark.gitlab.io/streams/structs/index.html</link><pubDate>Fri, 28 May 2021 23:00:36 +0000</pubDate><guid>https://robark.gitlab.io/streams/structs/index.html</guid><description>Structs and overloading operators #include &lt;iostream&gt; using namespace std; struct stud{ string name; int age; }; int main(){ stud a,b; a.name="bob"; a.age=19; b=a; cout&lt;&lt;b.name &lt;&lt; b.age; return 0; } Overriding ctor
#include &lt;iostream&gt; using namespace std; struct Stud{ Stud(); //default ctor Stud(string , int ); //variables optional string name; int age; }; Stud::Stud(){} Stud::Stud(string n, int a){ name=n; age=a; } int main(){ Stud a; Stud b("mary",22); a.name="bob"; a.age=19; cout&lt;&lt;b.name &lt;&lt; b.age; b=a; cout&lt;&lt;b.name &lt;&lt; b.age; return 0; } Initialization lists</description></item><item><title>Argv/try/catch</title><link>https://robark.gitlab.io/streams/args/index.html</link><pubDate>Tue, 01 Jun 2021 22:47:18 +0000</pubDate><guid>https://robark.gitlab.io/streams/args/index.html</guid><description>Command line arguments and Try Catch Argc Argv
#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;fstream&gt; using namespace std; bool vec2file(const vector&lt;string&gt; &amp;s, string filename) { //write vector to a file ofstream fout(filename); for (unsigned int x=0; x&lt;s.size(); x++) { fout &lt;&lt; s[x] &lt;&lt; endl; } fout.close(); return true; } vector&lt;string&gt; file2vec(string filename) { //read a file and return vector vector &lt;string&gt; strvect; string line; ifstream fin(filename); while (getline(fin,line)) { strvect.push_back(line); } fin.close(); return strvect; } int main(int argc, char* argv[]) { string fname = argv[1]; vector &lt;string&gt; food = file2vec(fname); string grocery(""); while (1) { cout&lt;&lt;"enter grocery to buy or quit to end: "; getline(cin, grocery); if (grocery == "quit") break; food.push_back(grocery); } cout&lt;&lt;"\nprinting list of groceries to buy"&lt;&lt;endl; for (unsigned int y=0; y&lt;food.size(); y++) { cout &lt;&lt; food[y] &lt;&lt; endl; } vec2file(food, fname); return 0; } Try/Catch</description></item></channel></rss>