Solved

File Input/Output

Posted on 2000-04-21
6
211 Views
Last Modified: 2010-04-10
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?
0
Comment
Question by:Mr_Wicked
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2738240
string the_name;
cout << "Give the file name"<<endl;
cin >> the_name

fistream thefile(the_name.c_str());
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2738246
Sorry, it's ifstream and not fistream
0
 

Author Comment

by:Mr_Wicked
ID: 2738289
It will open a file with what extension?
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Mr_Wicked
ID: 2738300
Can you explain the code you posted?
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 60 total points
ID: 2738751
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
 

Author Comment

by:Mr_Wicked
ID: 2738946
i'll check if this works
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now