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 streamThat should solve your problem.