devxlogo

I/O stream and opening to a file

I/O stream and opening to a file

Question:
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          aborted.";  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?

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

char fileName [100]; // holds the filenamecout << "enter file name " ;cin >> fileName;ifstream if (fileName);// .. code to read from the stream
That should solve your problem.

See also  Small Business Strategies with Venmo
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist