Advertisement
TechnoSam

File I/O C++

Jul 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. std::ifstream inFile;
  2. inFile.open(filename);
  3. std::string buffer = ""; // a full line of data
  4. std::string tokenA = ""; // first part of buffer
  5. std::string tokenB = ""; // second part of buffer
  6.  
  7. int coeff, power;
  8.  
  9. // Verify that the file is good
  10.  
  11. while (inFile.good()) {
  12.  
  13.     std::getline(inFile, buffer, '\n');
  14.     std::stringstream ss(buffer);
  15.     if (buffer == "") { break; }
  16.  
  17.     std::getline(ss, tokenA, ' ');
  18.     std::getline(ss, tokenB, ' ');
  19.  
  20.     try {
  21.         coeff = std::stoi(tokenA);
  22.         power = std::stoi(tokenB);
  23.     } catch (...) {
  24.         std::cout << "Only integers are allowed" << endl;
  25.         return EXIT_FAILURE
  26.         // Or maybe System.exit(1), my memory is fuzzy
  27.     }
  28.  
  29.     // Insert the values into the linked list, possibly inside a wrapper
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement