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

Problem of Assignment 3 Identify and correct the errors in the code segments in

ID: 3663585 • Letter: P

Question

Problem of Assignment 3

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

Solution for Assignment 3:

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

a)                              

fstream myfile("info.dat"); // error

if(myfile)

{

cout << "Error in file opening.";

exit(1);

}

Corrected answer:

ifstream myfile("info.dat");

if(myfile)

{

cout << "Error in file opening.";

exit(1);

}

b)

ifstream fstr;

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

Corrrected Answer:

ifstream fstr;

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

Either we can change ios::out to ios::in or ifstream to ofstream.

d)

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

fin<<obj;

fin.close();

Corrected Answer:

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

fin<<obj;

fin.close();

e)

ofstream fstr("info.dat"); // error

fstr>>x>>y;

fstr.close();

Corrected answer:

ifstream fstr("info.dat");

fstr>>x>>y;

fstr.close();

f)

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

fout<<a;

Ans: No error

====================================================================

2. State the output produced by the following program

#include<fstream>

#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;

}

Output:

S

G

Explanation:

ftest2.seekg(-8, ios::end); points to char S in OOP USING C++

ftest2.seekg(2, ios::cur); This moves the pointer from current pos to 2 positions. So pointer moves from S to G in OOP USING C++.

======================================================================

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

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

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

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

ifstream object before you can read your input data Ans: True

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

further access to the file. Ans: True

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

sophisticated C++ I/O system. Ans: No

=======================================================================

4. True or false, assuming

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

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

the keyboard. Ans: False

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

characters, our program will crash. Ans: False, It will wait for our fourth input.

4. All computers recognize ASCII code. Ans: True

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