RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Jun 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

I/O stream and opening to a file

I have a completed program coded as a bubble sort. I must now read and write to a file. I am using fstream.h and coding an input for the user to enter a file name. After a name is entered, the program terminates because no file exists. My question (see block of code):
void displaydata ( char** aszData){
 int getdata (char ** aszData, int nMax){
  char szBuf[100]
  ifstream fInput;
  cout <<"file could not be opened. Program
  exit (1);
I don't know how get the file name into the program. The program now sorts names. I am interested in letting the user input a file name, then have the program let names be entered and sort and finally save to file. Is this possible without getting too involved?

Here's what to do: ifstream should be constructed with a file name as an argument, like:

char fileName [100]; // holds the filename
cout << "enter file name " ;
cin >> fileName;
ifstream if (fileName);
// .. code to read from the stream
That should solve your problem.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date