Solved

File Input/Output

Posted on 2000-04-21
6
214 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
Independent Software Vendors: 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

Technology Partners: 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

737 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