Advertisement
Guest User

Untitled

a guest
May 30th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1.     // Initialise a stringstream to hold the loaded files contents.
  2.     ostringstream fileStream;
  3.  
  4.     bool fileAccepted = false;
  5.  
  6.     // Loop until the user inputs a valid filename or type "Exit".
  7.     do
  8.     {
  9.         string fileName;
  10.  
  11.         // Ask the user for a filename input for the program to load data from.
  12.         cout << "Please enter the filname. E.g. problem_small.txt. To exit, enter 'Exit'." << endl;
  13.         cin >> fileName;
  14.  
  15.         cin.get();
  16.  
  17.         // If the user types "Exit", exit the program.
  18.         if (fileName == "Exit")
  19.         {
  20.             return 0;
  21.         }
  22.  
  23.         // Open a ifstream with the filename from the user.
  24.         ifstream inFile(fileName);
  25.  
  26.         // Check if the file successfully loaded. This will only pass if the user enters a file that exists.
  27.         if (inFile)
  28.         {
  29.             // When the file is successfully loaded, transfer the data into the stringstream.
  30.             fileStream << inFile.rdbuf();
  31.  
  32.             // Close the opened file.
  33.             inFile.close();
  34.  
  35.             fileAccepted = true;
  36.         }
  37.         else
  38.         {
  39.             // Report that the file could not be loaded.
  40.             cout << "Could not find/load file " << fileName << "." << endl;
  41.         }
  42.  
  43.         // If the file has not been loaded, ask again. If it has been loaded, continue the program.
  44.     } while (!fileAccepted);
  45.  
  46.     // Get the stringstream into a usable format.
  47.     istringstream fileData(fileStream.str());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement