Solved

File Input/Output

Posted on 2000-04-21
6
209 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

9 Experts available now in Live!

Get 1:1 Help Now