assign40.pdf - #include <iostream>#include <fstream> using namespace std Get file name void getFileName(char fileName cout <"Enter source filename cin >

assign40.pdf - #include <iostream>#include <fstream> using...

This preview shows page 1 - 3 out of 3 pages.

#include <iostream> #include <fstream> using namespace std; /********************************************************************** * Get file name ***********************************************************************/ void getFileName( char fileName[]) { cout << "Enter source filename: " ; cin >> fileName; } /********************************************************************** * Display ***********************************************************************/ void display( char game[][3]) { cout << " " << game[0][0] << " | " << game[0][1] << " | " << game[0][2] << " " << endl << "---+---+---" << endl << " " << game[1][0] << " | " << game[1][1] << " | " << game[1][2] << " " << endl << "---+---+---" << endl << " " << game[2][0] << " | " << game[2][1] << " | " << game[2][2] << " " << endl; } /********************************************************************** * This function will read the file. ************************************************************************/ char readFile( char fileName[], char game[][3]) { //open ifstream fin(fileName); //fail to open if (fin.fail()) { cout << "Error reading file \"" << fileName << "\"" << endl; return -1; } //read file char row; for ( int
Image of page 1
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

Unformatted text preview: i = 0; i &lt; 3; i++) { for ( int j = 0; j &lt; 3; j++) { fin &gt;&gt; row; if (row == '.' ) game[i][j] = ' ' ; else game[i][j] = row; } } //close file fin.close(); return 0; } /**************************************************************** * write into new file ******************************************************************/ void writeFile( char game[3]) { //Delcare file-output ofstream fout; char newFileName[256]; // Ask for filename cout &lt;&lt; &quot;Enter destination filename: &quot; ; cin &gt;&gt; newFileName; //Open destination file &amp; error checking fout.open(newFileName); if (fout.fail()) cout &lt;&lt; &quot;fail to open file.\n&quot; ; else cout &lt;&lt; &quot;File written\n&quot; ; //write game to file for ( int i = 0; i &lt; 3; i++) { for ( int j = 0; j &lt; 3; j++) { if (game[i][j] == ' ' ) fout &lt;&lt; '.' ; else fout &lt;&lt; game[i][j]; } } //close fout.close(); } /************************************************************ * get file name, read file and display *********************************************************/ int main() { char fileName[256]; getFileName(fileName); char game[3][3]; readFile(fileName, game); display(game); writeFile(game); return 0; }...
View Full Document

  • Spring '16
  • CHRISTEN UNSICKER

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

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors