Introduction to the _c++ program design of NetEase Cloud Classroom (Next) _ Unit 7th: In and out of the same interest, all the appropriate – file input and Output _ unit 7th-Job 2: Programmatic Mutual evaluation

Source: Internet
Author: User

Unit 7th-Assignment 2: Programmatic Mutual evaluation

View Help

return

Submit Job (10 days remaining)

complete and submit the job

Homework correction

Mutual Assessment TrainingMutual evaluation work Self-assessment work

Results announcement

View Results

Because the online programming does not support the file operation, the work of this unit needs to be done by mutual evaluation.

In the evaluation criteria, there will be an analysis of the problem, so, want to know how to do the students, must participate in mutual evaluation

This assignment has two questions, practice the text mode file IO and the binary file IO respectively.

In accordance with the academic integrity clause, I guarantee that this answer is my original, all the external materials quoted in the answer have been marked by the source.

1 (10 points)

Please refer to "section X of Unit 7th: Description of unit work 2nd" in this module

Please submit the code (CPP file only).

The analytic results of this job are in the scoring standard of mutual evaluation. Therefore, the students who want to know how to do this problem, be sure to conduct mutual evaluation .

The answer is more than 5000 words recommended to use the attachment form upload

#include <fstream> #include <iostream> #include <limits>using namespace std;class screen {private://- ---to replenish multiple data domain members//Get/Release graphics window resources in the screen class, is a Raii method//For RAII, see Exception Handling unit material unsigned int width;unsigned int height;static Screen *instance; screen (unsigned int width, unsigned int height) {//If the graphics library is enabled, the function that initializes the graphics mode is placed here//Initgraph (Width_, Height_);this-> width = width;this->height = height;}; ~screen () {///If the graphics library is enabled, the function to turn off graphics mode is placed here//Closegraph ();d elete instance;instance = NULL;} public://----Supplement GetWidth () with getheight () function, unsigned int getwidth () {return width;} unsigned int getheight () {return height;} Static screen* getinstance (unsigned int width = 640, unsigned int height = 480) {//Singleton mode//----supplemental function body screen* ret = instance; if (ret = = NULL) {instance = new screen (width, height); ret = instance;} return ret;}}; screen* screen::instance = 0;//----The Special data member initialization statement of the screen class int main () {int width, height; screen* screen = 0;//7th Unit-Unit Job Title 2nd Description (Summary) _1. Save the file name "Screen.txt" with a String object, and then create the FStream object.Then call the Open function to open the file using read-write mode. String fileName = "Screen.txt"; fstream fs;//7th Unit-Unit Job Title 2nd description (verbose) _1. The file stream object is assumed to be fstream fs;fs.open (fileName, ios::in | ios::out);//Unit 7th-Unit Job description (verbose) _2. Read-write mode when opening a file, you need to use Ios::in | Ios::out as the parameter of the open function of the Stream object FS//Unit 7th-Unit Job Title 2nd Description (Summary) _2. A file opened using read-write mode, and if the file does not exist, it will fail to open. In this case, you need to use a separate write mode to open the file and then close the file to create an empty file with a length of 0. The file is then opened in read-write mode. Unit 7th-Unit Assignment Caption (detailed) _3. Determine if the file operation (open, read-write) is successful, using Fs.fail (). If successful, fail () returns FALSE, otherwise returns Trueif (Fs.fail ()) {//output hint Std::cout << "file opened using read-write mode, if the file does not exist, the failure will be turned on." << std::endl;//Open File Fs.open (filename, ios::out);//Close file Fs.close ();//Open file with read/write mode fs.open (filename, ios::in | Ios::out);} Unit 7th-Unit Assignment 2nd Description (summary) _3. From the open file, read the width and height of the screen. If the read fails, the width and height of the screen are read from the keyboard. Also, to clear the status bit of the file stream, all subsequent file operation codes will fail. Unit 7th-Unit Assignment Caption (detailed) _4. When the file is opened, if the file is empty (size 0), the data is read from the file stream and fails. You need to read the width and height of the screen from the keyboard. Then, you need to call the following function to clear the status bit of the file stream, otherwise all subsequent file operations will fail. Get file Size fs.seekg (0, ios_base::end), int nfilelen = FS.TELLG (), if (Nfilelen = = 0) {std::cin >> width >> height;f S.clear ();} ELSE{FS.SEEKG (Ios::beg); Move the file's read pointer to file header FS >> width >> height;} Unit 7th-Unit Assignment 2nd Description (summary) _4. Keep the code in the original main () function: Screen = screen::getinstance (width, height), screen = Screen::getinstance ();//Unit 7th-Unit Job 2nd Description (Summary) _5.  At the beginning of the file's head, write the width of the screen (Screen->getwidth ()) and the height of the screen (Screen->getheight ()) FS.SEEKP (Ios::beg); Move the file's write pointer to the file header fs << screen->getwidth () << "" << screen->getheight ();//Unit 7th-Unit Job Title 2nd Description (Summary) _6. If there is an error writing the file, the error message is then closed and the file is returned-1if (Fs.fail ()) {Std::cout << 6. If there is an error writing the file, the error message is then closed and the file is returned-1 "<< std::endl;fs.close (); return-1;} Unit 7th-Unit Assignment 2nd Description (summary) _7.  Reads the width and height of the screen just written from the file, and then displays the FS.SEEKG (Ios::beg) on the screen; Move the read pointer of the file to the file header fs >> width >> height;std::cout << width << "" << height;//7th Unit-Unit Job Title 2nd description (summary) _8. Finally close file Fs.close (); return 0;}

Introduction to the _c++ program design of NetEase Cloud Classroom (Next) _ Unit 7th: In and out of the same interest, all the appropriate – file input and Output _ unit 7th-Job 2: Programmatic Mutual evaluation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.