Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Initialise a stringstream to hold the loaded files contents.
- ostringstream fileStream;
- bool fileAccepted = false;
- // Loop until the user inputs a valid filename or type "Exit".
- do
- {
- string fileName;
- // Ask the user for a filename input for the program to load data from.
- cout << "Please enter the filname. E.g. problem_small.txt. To exit, enter 'Exit'." << endl;
- cin >> fileName;
- cin.get();
- // If the user types "Exit", exit the program.
- if (fileName == "Exit")
- {
- return 0;
- }
- // Open a ifstream with the filename from the user.
- ifstream inFile(fileName);
- // Check if the file successfully loaded. This will only pass if the user enters a file that exists.
- if (inFile)
- {
- // When the file is successfully loaded, transfer the data into the stringstream.
- fileStream << inFile.rdbuf();
- // Close the opened file.
- inFile.close();
- fileAccepted = true;
- }
- else
- {
- // Report that the file could not be loaded.
- cout << "Could not find/load file " << fileName << "." << endl;
- }
- // If the file has not been loaded, ask again. If it has been loaded, continue the program.
- } while (!fileAccepted);
- // Get the stringstream into a usable format.
- istringstream fileData(fileStream.str());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement