Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":13948,"date":"2015-05-06T14:22:16","date_gmt":"2015-05-06T19:22:16","guid":{"rendered":"https:\/\/mauriciocoopera.withknown.com\/2015\/mastery-28"},"modified":"2015-05-06T14:22:16","modified_gmt":"2015-05-06T19:22:16","slug":"mastery-28-11","status":"publish","type":"post","link":"https:\/\/kenscourses.com\/tc101winter2015\/2015\/mastery-28-11\/","title":{"rendered":"Mastery 28"},"content":{"rendered":"
\n

Reading and writing of files in C++<\/span><\/p>\n

Opening a File:<\/h2>\n

A file must be opened before you can read from it or write to it. Either the\u00a0ofstream<\/strong>\u00a0or\u00a0fstream<\/strong>object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.<\/p>\n

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.<\/p>\n

#d6d6d6;\">void<\/span> open<\/span>(<\/span>const<\/span> char<\/span> *<\/span>filename<\/span>,<\/span> ios<\/span>::<\/span>openmode mode<\/span>);<\/span><\/pre>\n

Here, the first argument specifies the name and location of the file to be opened and the second argument of the\u00a0open()<\/strong>\u00a0member function defines the mode in which the file should be opened.<\/p>\n\n\n\n\n\n\n\n\n
Mode Flag<\/th>\nDescription<\/th>\n<\/tr>\n
ios::app<\/td>\nAppend mode. All output to that file to be appended to the end.<\/td>\n<\/tr>\n
ios::ate<\/td>\nOpen a file for output and move the read\/write control to the end of the file.<\/td>\n<\/tr>\n
ios::in<\/td>\nOpen a file for reading.<\/td>\n<\/tr>\n
ios::out<\/td>\nOpen a file for writing.<\/td>\n<\/tr>\n
ios::trunc<\/td>\nIf the file already exists, its contents will be truncated before opening the file.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

You can combine two or more of these values by\u00a0OR<\/strong>ing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:<\/p>\n

#d6d6d6;\">ofstream outfile<\/span>;<\/span>\noutfile<\/span>.<\/span>open<\/span>(<\/span>\"file.dat\"<\/span>,<\/span> ios<\/span>::<\/span>out<\/span> |<\/span> ios<\/span>::<\/span>trunc <\/span>);<\/span><\/pre>\n

Similar way, you can open a file for reading and writing purpose as follows:<\/p>\n

#d6d6d6;\">fstream  afile<\/span>;<\/span>\nafile<\/span>.<\/span>open<\/span>(<\/span>\"file.dat\"<\/span>,<\/span> ios<\/span>::<\/span>out<\/span> |<\/span> ios<\/span>::<\/span>in<\/span> );<\/span><\/pre>\n

Closing a File<\/h2>\n

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.<\/p>\n

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.<\/p>\n

#d6d6d6;\">void<\/span> close<\/span>();<\/span><\/pre>\n

Writing to a File:<\/h2>\n

While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream\u00a0or\u00a0fstream<\/strong>\u00a0object instead of the\u00a0cout<\/strong>\u00a0object.<\/p>\n

Reading from a File:<\/h2>\n

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an\u00a0ifstream<\/strong>\u00a0or\u00a0fstream<\/strong>\u00a0object instead of the\u00a0cin<\/strong>\u00a0object.<\/p>\n

Read & Write Example:<\/h2>\n

Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:<\/p>\n

#d6d6d6;\">#include<\/a><\/span> <\/fstream><\/span>\n#include<\/a><\/span> <\/iostream><\/span>\nusing<\/span> namespace<\/span> std<\/span>;<\/span>\n \nint<\/span> main <\/span>()<\/span>\n{<\/span>\n    \n   char<\/span> data<\/span>[<\/span>100<\/span>];<\/span>\n\n   \/\/ open a file in write mode.<\/span>\n   ofstream outfile<\/span>;<\/span>\n   outfile<\/span>.<\/span>open<\/span>(<\/span>\"afile.dat\"<\/span>);<\/span>\n\n   cout <\/span> \"Writing to the file\"<\/span>  endl<\/span>;<\/span>\n   cout <\/span> \"Enter your name: \"<\/span>;<\/span> \n   cin<\/span>.<\/span>getline<\/span>(<\/span>data<\/span>,<\/span> 100<\/span>);<\/span>\n\n   \/\/ write inputted data into the file.<\/span>\n   outfile <\/span> data <\/span> endl<\/span>;<\/span>\n\n   cout <\/span> \"Enter your age: \"<\/span>;<\/span> \n   cin <\/span>>><\/span> data<\/span>;<\/span>\n   cin<\/span>.<\/span>ignore<\/span>();<\/span>\n   \n   \/\/ again write inputted data into the file.<\/span>\n   outfile <\/span> data <\/span> endl<\/span>;<\/span>\n\n   \/\/ close the opened file.<\/span>\n   outfile<\/span>.<\/span>close<\/span>();<\/span>\n\n   \/\/ open a file in read mode.<\/span>\n   ifstream infile<\/span>;<\/span> \n   infile<\/span>.<\/span>open<\/span>(<\/span>\"afile.dat\"<\/span>);<\/span> \n \n   cout <\/span> \"Reading from the file\"<\/span>  endl<\/span>;<\/span> \n   infile <\/span>>><\/span> data<\/span>;<\/span> \n\n   \/\/ write the data at the screen.<\/span>\n   cout <\/span> data <\/span> endl<\/span>;<\/span>\n   \n   \/\/ again read the data from the file and display it.<\/span>\n   infile <\/span>>><\/span> data<\/span>;<\/span> \n   cout <\/span> data <\/span> endl<\/span>;<\/span> \n\n   \/\/ close the opened file.<\/span>\n   infile<\/span>.<\/span>close<\/span>();<\/span>\n\n   return<\/span> 0<\/span>;<\/span>\n}<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/pre>\n

When the above code is compiled and executed, it produces the following sample input and output:<\/p>\n

#d6d6d6;\">$<\/span>.\/<\/span>a<\/span>.<\/span>out<\/span>\nWriting<\/span> to the file\n<\/span>Enter<\/span> your name<\/span>:<\/span> Zara<\/span>\nEnter<\/span> your age<\/span>:<\/span> 9<\/span>\nReading<\/span> from<\/span> the file\n<\/span>Zara<\/span>\n9<\/span><\/pre>\n

Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.<\/p>\n

Credits:<\/span><\/p>\n

<\/a>http:\/<\/wbr>\/<\/wbr>www.tutorialspoint.com\/<\/wbr>cplusplus\/<\/wbr>cpp_files_streams.htm<\/a><\/span><\/p>\n

#TC<\/a>1017 #Mastery<\/a>28<\/span><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

\n

Reading and writing of files in C++<\/span><\/p>\n

Opening a File:<\/h2>\n

A file must be opened before you can read from it or write to it. Either the ofstream<\/strong> or fstream<\/strong>object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.<\/p>\n

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.<\/p>\n

#d6d6d6;\">void<\/span> open<\/span>(<\/span>const<\/span> char<\/span> *<\/span>filename<\/span>,<\/span> ios<\/span>::<\/span>openmode mode<\/span>);<\/span><\/pre>\n

Here, the first argument specifies the name and location of the file to be opened and the second argument of the open()<\/strong> member function defines the mode in which the file should be opened.<\/p>\n\n\n\n\n\n\n\n\n
Mode Flag<\/th>\nDescription<\/th>\n<\/tr>\n
ios::app<\/td>\nAppend mode. All output to that file to be appended to the end.<\/td>\n<\/tr>\n
ios::ate<\/td>\nOpen a file for output and move the read\/write control to the end of the file.<\/td>\n<\/tr>\n
ios::in<\/td>\nOpen a file for reading.<\/td>\n<\/tr>\n
ios::out<\/td>\nOpen a file for writing.<\/td>\n<\/tr>\n
ios::trunc<\/td>\nIf the file already exists, its contents will be truncated before opening the file.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

You can combine two or more of these values by OR<\/strong>ing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:<\/p>\n

#d6d6d6;\">ofstream outfile<\/span>;<\/span>\noutfile<\/span>.<\/span>open<\/span>(<\/span>\"file.dat\"<\/span>,<\/span> ios<\/span>::<\/span>out<\/span> |<\/span> ios<\/span>::<\/span>trunc <\/span>);<\/span><\/pre>\n

Similar way, you can open a file for reading and writing purpose as follows:<\/p>\n

#d6d6d6;\">fstream  afile<\/span>;<\/span>\nafile<\/span>.<\/span>open<\/span>(<\/span>\"file.dat\"<\/span>,<\/span> ios<\/span>::<\/span>out<\/span> |<\/span> ios<\/span>::<\/span>in<\/span> );<\/span><\/pre>\n

Closing a File<\/h2>\n

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.<\/p>\n

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.<\/p>\n

#d6d6d6;\">void<\/span> close<\/span>();<\/span><\/pre>\n

Writing to a File:<\/h2>\n

While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream<\/strong> object instead of the cout<\/strong> object.<\/p>\n

Reading from a File:<\/h2>\n

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream<\/strong> or fstream<\/strong> object instead of the cin<\/strong> object.<\/p>\n

Read & Write Example:<\/h2>\n

Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:<\/p>\n

#d6d6d6;\">#include<\/a><\/span> <\/span>\n#include<\/a><\/span> <\/span>\nusing<\/span> namespace<\/span> std<\/span>;<\/span>\n \nint<\/span> main <\/span>()<\/span>\n{<\/span>\n    \n   char<\/span> data<\/span>[<\/span>100<\/span>];<\/span>\n\n   \/\/ open a file in write mode.<\/span>\n   ofstream outfile<\/span>;<\/span>\n   outfile<\/span>.<\/span>open<\/span>(<\/span>\"afile.dat\"<\/span>);<\/span>\n\n   cout <\/span> \"Writing to the file\"<\/span>  endl<\/span>;<\/span>\n   cout <\/span> \"Enter your name: \"<\/span>;<\/span> \n   cin<\/span>.<\/span>getline<\/span>(<\/span>data<\/span>,<\/span> 100<\/span>);<\/span>\n\n   \/\/ write inputted data into the file.<\/span>\n   outfile <\/span> data <\/span> endl<\/span>;<\/span>\n\n   cout <\/span> \"Enter your age: \"<\/span>;<\/span> \n   cin <\/span>>><\/span> data<\/span>;<\/span>\n   cin<\/span>.<\/span>ignore<\/span>();<\/span>\n   \n   \/\/ again write inputted data into the file.<\/span>\n   outfile <\/span> data <\/span> endl<\/span>;<\/span>\n\n   \/\/ close the opened file.<\/span>\n   outfile<\/span>.<\/span>close<\/span>();<\/span>\n\n   \/\/ open a file in read mode.<\/span>\n   ifstream infile<\/span>;<\/span> \n   infile<\/span>.<\/span>open<\/span>(<\/span>\"afile.dat\"<\/span>);<\/span> \n \n   cout <\/span> \"Reading from the file\"<\/span>  endl<\/span>;<\/span> \n   infile <\/span>>><\/span> data<\/span>;<\/span> \n\n   \/\/ write the data at the screen.<\/span>\n   cout <\/span> data <\/span> endl<\/span>;<\/span>\n   \n   \/\/ again read the data from the file and display it.<\/span>\n   infile <\/span>>><\/span> data<\/span>;<\/span> \n   cout <\/span> data <\/span> endl<\/span>;<\/span> \n\n   \/\/ close the opened file.<\/span>\n   infile<\/span>.<\/span>close<\/span>();<\/span>\n\n   return<\/span> 0<\/span>;<\/span>\n}<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/pre>\n

When the above code is compiled and executed, it produces the following sample input and output:<\/p>\n

#d6d6d6;\">$<\/span>.\/<\/span>a<\/span>.<\/span>out<\/span>\nWriting<\/span> to the file\n<\/span>Enter<\/span> your name<\/span>:<\/span> Zara<\/span>\nEnter<\/span> your age<\/span>:<\/span> 9<\/span>\nReading<\/span> from<\/span> the file\n<\/span>Zara<\/span>\n9<\/span><\/pre>\n

Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.<\/p>\n

Credits:<\/span><\/p>\n

<\/a>http:\/\/www.tutorialspoint.com\/cplusplus\/cpp_files_streams.htm<\/a><\/span><\/p>\n

#TC<\/a>1017 #Mastery<\/a>28<\/span><\/p>\n<\/div>\n

Continue reading →<\/span><\/a><\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3,26],"tags":[161,395,520,519,517,144,396,521,397,394,234,497,213,95,127,486,40,287],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/posts\/13948"}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/comments?post=13948"}],"version-history":[{"count":6,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/posts\/13948\/revisions"}],"predecessor-version":[{"id":19151,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/posts\/13948\/revisions\/19151"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/media?parent=13948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/categories?post=13948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101winter2015\/wp-json\/wp\/v2\/tags?post=13948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}