In: Computer Science
Assuming that "out_file" is an ofstream object, what is the correct way of opening a file "Employees.txt" for writing?
Group of answer choices
out_Employees.open(Employees.txt);
out_file(Employees.txt);
out_file("Employees.txt");
out_file.open("Employees.txt");
Flag this Question
Question 241 pts
What is wrong with the following function definition?
void erase_file(ifstream infile, ofstream outfile) { char ch; while (infile.get(ch)) { outfile << ch; } }
Group of answer choices
The ifstream and ofstream parameters must be reference parameters.
The function does not read an end of line character to terminate the while loop.
The output from the function does not display all of the characters in the file.
The ifstream and ofstream parameters must be pointers.
Flag this Question
Question 251 pts
Consider the following code snippet:
1. int main() 2. { 3. char ch; 4. ifstream in_file; 5. in_file.open("C:\names.txt"); 6. while (in_file.get(ch)) 7. { 8. cout << ch; 9. } return 0; }
Which of the following statements describes the problem with this code snippet?
Group of answer choices
The program contains an infinite loop.
There is a syntax error in line 6.
The variable in_file is never assigned a value.
Line 5 does not open the file C:\names.txt.
Please up vote ,comment if any query . Thanks for question .Be safe .
Answer : out_file.open("Employees.txt");
Assuming that "out_file" is an ofstream object, what is the correct way of opening a file "Employees.txt" for writing?
Group of answer choices
out_Employees.open(Employees.txt);
out_file(Employees.txt);
out_file("Employees.txt");
out_file.open("Employees.txt");
Explanation :
when we declare filestream object ,created object takes file name like below example
ex- ofstream out_file("employees.txt") //this will open file
but ofstream out_file ; this statement create object but we did not pass any file name .
when we will try out_file("employee.txt") ; //will throw error when program compile
right statement is :
ofstream out_file ; //this is default constructor which takes no parameter
out_file.open("employees.txt") ;//this is class method of ofstream which open file
or we can try using parameter constructor
ofstream out_file("emloyees.txt"); //this parameter constructor takes file name and open it
Question 241 :
What is wrong with the following function definition?
void erase_file(ifstream infile, ofstream outfile) { char ch; while (infile.get(ch)) { outfile << ch; } }
Answer : The ifstream and ofstream parameters must be reference parameters.
Explanation :
when we pass file stream object to function it takes reference of object . so we have to pass reference to function .
also we have to change function signature .
like this :
//reference paramter using & operator
void erase_file(ifstream& infile, ofstream&
outfile)
{
char ch;
while (infile.get(ch))
{
outfile << ch;
}
Question 251 pts
Consider the following code snippet:
1. int main() 2. { 3. char ch; 4. ifstream in_file; 5. in_file.open("C:\names.txt"); 6. while (in_file.get(ch)) 7. { 8. cout << ch; 9. } return 0; }
Answer : Line 5 does not open the file C:\names.txt.
Explanation :
in_file.open("C:\names.txt"); here we have error it will not open file , still file stream object is null .
change we have to do is replace \ with \\
in_file.open("C:\\names.txt"); // we used \\ instead of 1
in c++ we have to replace with two forword slash char ,some language supports single slash .
Please up vote ,comment if any query .