Question

In: Computer Science

Assuming that "out_file" is an ofstream object, what is the correct way of opening a file...

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.

Solutions

Expert Solution

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 .


Related Solutions

Describe how to open a file in Excel? What is the difference between opening a file...
Describe how to open a file in Excel? What is the difference between opening a file and creating a new file in Excel?
what is the correct way of doing a anatomy lab report?
what is the correct way of doing a anatomy lab report?
What is the correct way to measure NGT and how is it inserted into the patient?...
What is the correct way to measure NGT and how is it inserted into the patient? In what 3 ways can the TNG (if it is in place) be checked for patent? What can be a complication for the patient of not placing the tube correctly? What are the nursing interventions (management) with these patients?
what is the correct way to annualize an interest rate in financial decisions?
what is the correct way to annualize an interest rate in financial decisions?
Assuming the correct tables are created for product and manufacturer, describe what needs to be done...
Assuming the correct tables are created for product and manufacturer, describe what needs to be done to specify one-to-many relationship between manufacturer and product in Laravel’s Eloquent.
A file filter reads an input file, transforms it in some way, and writes the results...
A file filter reads an input file, transforms it in some way, and writes the results to an output file. Write an abstract file filter class that defines a pure virtual function for transforming a character. Create one subclass of your file filter class that performs encryption, another that transforms a file to all uppercase, and another that creates an unchanged copy of the original file. The class should have a member function void doFilter(ifstream &in, ofstream &out) that is...
Compilers and Assemblers translate each source file individually to generate object code files. Hence the object...
Compilers and Assemblers translate each source file individually to generate object code files. Hence the object files need to be linked together before they can be executed. Theoretically, however, it is possible to skip the linking step and directly have compilers generate the final executable file. What would be the downside of taking the latter approach
Is this the correct way to differentiate the sampling techniques? Is my cluster vs stratified correct?...
Is this the correct way to differentiate the sampling techniques? Is my cluster vs stratified correct? Convenience Sampling- a sampling technique where you are sampling with the intent of saving time, money, or simply making the sample easier to get it over with Simple Random Sample- Everybody is equally likely to get picked Systematic Sampling- Sampling where there are exploitable patterns. Ex: Every 7th customer wins a car Cluster Sampling- A population of interest is broken down into these things...
Consider the following header file for the intArray object and the following main.cpp and answer the...
Consider the following header file for the intArray object and the following main.cpp and answer the following questions: intArray.h main.cpp #include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(int _size);     intArray(int _size, int array[]);     intArray(const intArray&);     ~intArray();     void Print();     void PrintSize();     int Size() const;     int operator[](int) const;     intArray operator=(const intArray ); private:     int size;     int *data; }; 7 #include <iostream>...
An object of 4 kg (assuming the acceleration of gravity g = 10 m / s2...
An object of 4 kg (assuming the acceleration of gravity g = 10 m / s2 ) suspended from a spring causes the spring to stretch 2 cm downwards. The object is moved 3 cm down from from its equilibrium position in the positive direction of movement, then it is released with speed zero initial. Assuming that there is no damping and that the object is under external force equal to: 20cos4(t) (N) a) Formulate the initial value problem that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT