C++ file operation challenge

profilesankouh89

1. Create a text file and copy this text to it:

Shall I compare thee to a summer's day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date:
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance, or nature's changing course, untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou ow'st;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou grow'st;
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.

2. Add the following code to your Main():

int main() {

 std::ifstream in_file {"../poem.txt"};

 std::ofstream out_file{"../poem_out.txt"};

 if (!in_file) {

 std::cerr << "Error opening input file" << std::endl;

 return 1;

 }

 if (!out_file) {

 std::cerr << "Error opening output file" << std::endl;

 return 1;

 }

 std::string line{};

 while (std::getline(in_file, line))

 out_file << line << std::endl;

 std::cout << "File copied" << std::endl;

 in_file.close();

 out_file.close();

 return 0;

}

3. Analyze the code, fully understand it, and Change the code to manipulate the file and add a line number to the beginning of each line.

your output should look like this:

1 Shall I compare thee to a summer's day?
2 Thou art more lovely and more temperate:
3 Rough winds do shake the darling buds of May,
4 And summer's lease hath all too short a date:
5 Sometime too hot the eye of heaven shines,
6 And often is his gold complexion dimm'd;
7 And every fair from fair sometime declines,
8 By chance, or nature's changing course, untrimm'd;
9 But thy eternal summer shall not fade
10 Nor lose possession of that fair thou ow'st;
11 Nor shall Death brag thou wander'st in his shade,
12 When in eternal lines to time thou grow'st;
13 So long as men can breathe or eyes can see,
14 So long lives this, and this gives life to thee.

4. Copy your code and paste it here (text input).

    • 7 years ago
    • 30
    Answer(2)

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      order_117799_3026751.doc

    Purchase the answer to view it

    blurred-text
    NOT RATED
    • attachment
      cpp-copy-files-line-nos.zip