File Input/Output

Hi. I have like 25 files with info on my machine. What I want to do is create a program that a person enters the name of a file and only that file would open and be read and its contents output.

for example some file names:

war.txt
clean.txt
america.txt
baseball.txt
republican.txt


I want the user to enter a name of a file. Lets say the user types war. I want to be able to read only the contenst of this file. Is there a way I can do this without having to do lost of switch statements or if statements?
Mr_WickedAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
KangaRooConnect With a Mentor Commented:
It's just brief, if you don't want the user to type the extension

#include<string> // for the string class
#inlcude<fstream> // for file streams

// ....

string the_name;  // string class

// ask the user for the filename
//
cout << "Give the file name"<<endl;
cin >> the_name

// append the extension
//
the_name += string(".txt");

// open the filestream for reading
//
ifstream thefile(the_name.c_str());
// ifstream constructor takes a null terminated c string, which is obtained
// from the_name by it's member function c_str()
0
 
KangaRooCommented:
string the_name;
cout << "Give the file name"<<endl;
cin >> the_name

fistream thefile(the_name.c_str());
0
 
KangaRooCommented:
Sorry, it's ifstream and not fistream
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Mr_WickedAuthor Commented:
It will open a file with what extension?
0
 
Mr_WickedAuthor Commented:
Can you explain the code you posted?
0
 
Mr_WickedAuthor Commented:
i'll check if this works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.