once you are ready, you’ll need to do two things to make a connection to a users
ID: 3915075 • Letter: O
Question
once you are ready, you’ll need to do two things to make a connection to a users file: Time: Due R Calculator: OK Test 4 - Aggregation 30) Belore you can open a connection to a file stream, you must do three things A) declare a vector of objects to store the file contents. B) get from the user the name of their te. C) include the library fstream D) deciare either an ifstream or an ofstream variable to connect to the file with. E) declare a variable to count how many ines we've processed in the file. 31) Once you are ready, youn' need to do two things to make a connection to a users so: A) clear the stream variable to make sure it is ready to use. B) convert the string flename variable to a c.str as you pass it to the file connecting method C) pass your string filename to the file oonnecting method as is D) use the connect method with respect to your stream variable. E) use the open method with respect to your stream variablo. 32) When opening a Ne connection, make sure the stream connected successfully to the fhe by A) clear the filename string inside the checking loop. B) checking for fail or Igood or even just t C) use instead of getline for the filename the second [and later] time(s. D) clearing the stream variable after the checking loop is done. E) using close and clear inside the checking loop. 33) When calling code that might an exception, we should to execute it and then any possible exception(s). 34) TRUE/FALSE exceptions are meant to handle extreme situations. TRUE/FALSE For instance, many string class methods use them to report Invald capitalization of data. TRUE/ FALSE The methods of the vector class use them to report out.of.range positions 35) What functions/operations can you use to get data from an ifstream variable? What functions/operations can you use to put data into an ofstream variable? 36) What is the type of getline's first argument that it can be used to read from either cin or an ifstream vari- able? What type should you use to allow a function to display' to either cout or an ofstream variable?Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void connect(const char *cstr1){
string line;
ifstream f;
f.clear();
f.open(cstr1);
cout << "Reading from the file" << endl;
while ( getline (f,line) )
{
cout << line << ' ';
}
f.close();
}
int main()
{
string fileName;
cout << "Please Enter File Name: " << endl;
cin >> fileName ;
cout << "You Entered " << fileName << endl;
connect(fileName.c_str());
return 0;
}
Check the above code
1. f.clear(); : statment clear the stream variable where f is ifstream class varaible and clear is method of ifstream class.
2. fileName.c_str() : statment given in above code converts string into character array. c-str method is responsible for this conversion.
3. If you check the above code then you found taht there is one mehtod called connect is there and that method is being invoked from main method which is taking character array as file namespace
connect(fileName.c_str());
4. pleasse refer the connect method in above code which takes character array as file name and connect the file name and read the file content and print the file content.
5. f.open(cstr1): this instruction is being used in connect method where file content is neing read line by line . so te read any file we need to construct the input stream class object and invoke the open method that makes the connection to file and then pointer start file content line by line.
if you need any further help on given code then please let me know. i can walk through you the whole code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.