Solved

File Input/Output

Posted on 2000-04-21
6
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

687 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