printing out data from a user-defined text file

A

amanda.schwenk

So, I'm writing a program for my beginning C++ class and I am VERY
confused as to how to get the user defined file name opened and read
the data i need to from it.

i know i have print to the user what i want them to enter,

cin the filename

but where i get stuck is when it comes to inStream because if we don't
know the predetermined name of the file what goes in the quotes?
 
M

Mike Wahler

So, I'm writing a program for my beginning C++ class and I am VERY
confused as to how to get the user defined file name opened and read
the data i need to from it.

i know i have print to the user what i want them to enter,

cin the filename

but where i get stuck is when it comes to inStream because if we don't
know the predetermined name of the file what goes in the quotes?

Don't use quotes. Use the object into which the file
name was stored via cin.

std::string name;
std::cout << "File name? ";
std::cin >> name;
std::ifstream infile(name.c_str());

Admittedly, it would be nicer if the 'ifstream'
constructor took a std::string argument directly,
why this wasn't done has been the subject of
past discussions here, you can check the archives
if interested.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top