Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Identify and correct the errors in the code segments in Exercises a) through f)

ID: 3669183 • Letter: I

Question

Identify and correct the errors in the code segments in Exercises a) through f)

a)                              

fstream myfile("info.dat");

if(myfile)

{

cout << "Error in file opening.";

exit(1);

}

b)

ifstream fstr;

fstr.open("A:info.dat", ios::out);

c)

ofstream fout("comparts.dat");

if(fout.fail())

{

cerr<<"Error in file opening.";

exit(1);

}

fout.seekp(-2*sizeof(compart), ios::end);

fout.read((char*)&part.sizeof(compart));

d)

ifstream fin("info.dat", ios::app);

fin<<obj;

fin.close();

e)

ofstream fstr("info.dat");

fstr>>x>>y;

fstr.close();

f)

fout.seekg(n*sizeof(int),ios::cur);

fout<<a;

2. State the output produced by the following program

#include<iostrea>

#include<iostream>

using namespace std;

int main()

{

char *course="OOP USING C++", ch;

ofstream ftest1("test.dat");

ftest1<<course;

ftest1.close();

ifstream ftest2("test.dat");

ftest2.seekg(-8, ios::end);

ch=ftest2.get(); //reads a character

cout<<ch<<endl;

ftest2.seekg(2, ios::cur);

ch=ftest2.get(); //reads a character

cout<<ch;

return 0;

}

3. True or char c1, c2, c3, c4;

1. We use the infile function to read input from the keyboard.

2. We use the infile function to read input from a file.

3. You must create a link between an external disk file and an

ifstream object before you can read your input data

4. It is good practice to close an input file when you need no

further access to the file.

5. It is not possible to make input errors with the

sophisticated C++ I/O system.

4. True or false, assuming

1. Space (ASCII value 32) can be treated as a char.

2. We must put space between characters when we type them in at

the keyboard.

3. If we have cin>>c1>>c2>>c3>>c4; and type in only three

characters, our program will crash.

4. All computers recognize ASCII code.

Explanation / Answer

a.

fstream myfile("info.data");

// use of is_open function to check if file is opened

if (!myfile.is_open()) {

cout << "Error in file opening." << endl;

exit(1);

}

b.

fstr.open("A:\info.dat", ios::out); // escape the "", program compiles successfully!

c.

// when inputing from file input file stream (ifstream) is used

change "ofstream" to "ifstream"

there is no function called "seekp" for ifstream it is "seekg"

d.

// when outputting to file output file stream (ofstream is used)

ofstream fout("info.dat", ios::app);

fout <<obj;

fout.close();

e.

// use input file stream while reading from file

change "ofstream" to "ifstream"

f.

There is no member function such as "seekg" to ofstream it is seekp

fout.seekp(n*sizeof(int),ios::cur);

NOTE: Please post other two as separate questions.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote