Hello could you please answer this C question Write a progra

Hello, could you please answer this C++ question?

Write a program that creates file myStory.txt. Write the following lines out to the file:

I love to code

I really love to code in C++

Coding makes me very happy

Give me more code to write

The program should then read in the file myStory.txt and display it to the screen.

First, describe your algorithm using pseudocode.

Translate the pseudocode into a C++ program.

Solution

I have given two solutions of problem, both are correct but there are 2 separate approach

Pseudocode:1

Pseudocode:2

/////////////////////////////////////////////////////////////////////////////////// c++ Program //////////////////////////////////////////

#include <iostream>
#include <fstream> //header file used to create ifsteram/ofstream objects

using namespace std;

const unsigned bufferSize = 100; //buffer size used to read data from file
int main() {
    const char fileName[] = \"./myStory.txt\";   //char array for file name
    ofstream fout;     //output file stream object
    cout << \"Opening File \" << fileName << \" for Writing in Truncate mode(if file already exist content in file will be deleted).\" << endl;
    fout.open(fileName, ios::out | ios::trunc);
    fout << \"I love to code\" << endl;
    fout << \"I really love to code in C++\" << endl;
    fout << \"Coding makes me very happy\" << endl;
    fout << \"Give me more code to write\" << endl;
    fout.close();

    ifstream fin;    //Input file stream object
    cout <<\"Opening File \" << fileName << \" For Reading.\" << endl;
    fin.open(fileName, ios::in);
    char outBuffer[bufferSize] = {0};
    cout << \"Content in file is:\" << endl;
    while(fin.getline(outBuffer, bufferSize)) //read data in buffer and convert newline \'\ \' to null character \'\\0\'
    {
        cout << outBuffer << endl;
    }
    fin.close();
    return 0;
}

///////////////////////////////////Program 2 /////////////////////////

#include <iostream>
#include <fstream>

using namespace std;

const unsigned bufferSize = 100;
int main() {
    const char fileName[] = \"./myStory.txt\";
    fstream file;
    cout << \"Opening File \" << fileName << \" for Writing and Reading in Truncate mode(if file already exist content in file will be deleted).\" << endl;
    file.open(fileName, ios::out | ios::in | ios::trunc);
    file << \"I love to code\" << endl;
    file << \"I really love to code in C++\" << endl;
    file << \"Coding makes me very happy\" << endl;
    file << \"Give me more code to write\" << endl;

    cout <<\"Reading File \" << fileName << \" From Starting.\" << endl;
    file.seekg(0, ios::beg);
    char outBuffer[bufferSize] = {0};
    cout << \"Content in file is:\" << endl;
    while(file.getline(outBuffer, bufferSize))
    {
        cout << outBuffer << endl;
    }
    file.close();
    return 0;
}

Hello, could you please answer this C++ question? Write a program that creates file myStory.txt. Write the following lines out to the file: I love to code I rea
Hello, could you please answer this C++ question? Write a program that creates file myStory.txt. Write the following lines out to the file: I love to code I rea

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site