Identify and correct the errors in the code segments in Exercises a) through f)
ID: 3669528 • 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
1)
No error .. just add
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;
before the code
2) NO error
3)
temp.cpp:20:22: error: ‘compart’ was not declared in this scope
fout.seekp(-2*sizeof(compart), ios::end);
^
temp.cpp:22:6: error: ‘std::ofstream’ has no member named ‘read’
fout.read((char*)&part.sizeof(compart));
^
temp.cpp:22:19: error: ‘part’ was not declared in this scope
fout.read((char*)&part.sizeof(compart));
^
temp.cpp:22:24: error: expected unqualified-id before ‘sizeof’
fout.read((char*)&part.sizeof(compart));
^
4)
temp.cpp: In function ‘int main()’:
temp.cpp:10:6: error: ‘obj’ was not declared in this scope
fin<<obj;
e) temp.cpp: In function ‘int main()’:
temp.cpp:10:7: error: ‘x’ was not declared in this scope
fstr>>x>>y;
^
temp.cpp:10:10: error: ‘y’ was not declared in this scope
fstr>>x>>y;
f) temp.cpp: In function ‘int main()’:
temp.cpp:8:1: error: ‘fout’ was not declared in this scope
fout.seekg(n*sizeof(int),ios::cur);
^
temp.cpp:8:12: error: ‘n’ was not declared in this scope
fout.seekg(n*sizeof(int),ios::cur);
^
temp.cpp:10:7: error: ‘a’ was not declared in this scope
fout<<a;
^
-----------------------------------------------------------------------------------------------
2) OUTPUT of the given program
S
G
-----------------------------------------------------------------------------
3)TRUE/FALSe
False
Treue
True
True
False
------------------------------------------------------------------------------------------------------------
4. True or false, assuming
1. Space (ASCII value 32) can be treated as a char. -> True
2. We must put space between characters when we type them in at
the keyboard.->False
3. If we have cin>>c1>>c2>>c3>>c4; and type in only three
characters, our program will crash. -> False
4. All computers recognize ASCII code. -> False -> there are other data formats as well
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.