Advertisement
Guest User

parser

a guest
Oct 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. // Written by Garrett Gutierrez
  2. // For use in CSE240
  3.  
  4. #include <iostream>
  5. // Include fstream to import file reading functions.
  6. #include <fstream>
  7. // Include string to do string operations.
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. // Forward Declarations
  13.  
  14. void parseMethod1();
  15. void parseMethod2();
  16.  
  17. int main()
  18. {
  19.     char choice;
  20.     do{
  21.         cout << "Select parsing method" << endl;
  22.         cout << "(1) For using getline with delimeters" << endl;
  23.         cout << "(2) For using line parser" << endl;
  24.         cout << "(0) To quit" << endl;
  25.  
  26.         choice = getc(stdin);
  27.         cout << "\n";
  28.         if (choice == '1')
  29.             parseMethod1();
  30.         else if (choice == '2')
  31.             parseMethod2();
  32.         else
  33.         {
  34.             cout << "Incorrect input\n" << endl;
  35.             cin.ignore();
  36.         }
  37.            
  38.     } while (choice != '0');
  39.     return 0;
  40. }
  41.  
  42. // This function uses multiple get lines to read from the file
  43. void parseMethod1()
  44. {
  45.     // Declare object to read file. Enter the name of the file.csv you want to read from.
  46.     ifstream file("parsing.csv");
  47.     // These strings are declared to store the parsed data. There should be one variable per column of data
  48.     string firstName, lastName, eyeColor, height, weight, DOB;
  49.     int heightValue;
  50.     float weightValue;
  51.  
  52.     // If the file is readable
  53.     if (file.is_open())
  54.     {
  55.         // Keep reading until the delimeter. In this case, it is '/'
  56.         while (getline(file, firstName, ','))
  57.         {
  58.             getline(file, lastName, ',');
  59.             getline(file, eyeColor, ',');
  60.             getline(file, height, ',');
  61.             getline(file, weight, ',');
  62.             getline(file, DOB, ',');
  63.  
  64.             // Convert string to an integer
  65.             heightValue = stoi(height);
  66.             // Convert string to a float
  67.             weightValue = stof(weight);
  68.  
  69.             cout << firstName << ' ' << lastName << endl;
  70.             cout << "Eye color: " << eyeColor << endl;
  71.             cout << "Height in inches: " << heightValue << endl;
  72.             cout << "Weight in lbs: " << weightValue << endl;
  73.             cout << "Date of Birth (DD/MM/YYYY): " << DOB << "\n" << endl;
  74.  
  75.         }
  76.         // Always close a file when done reading.
  77.         file.close();
  78.     }
  79.     else
  80.         cout << "Unable to open file" << endl;
  81.     // Consumes extra newline at the end
  82.     cin.ignore();
  83. };
  84.  
  85. // This method gets an entire row from the file, then parses it for the desired information.
  86. void parseMethod2()
  87. {
  88.     // Declare object to read file. Enter the name of the file.csv you want to read from.
  89.     ifstream file("parsing.csv");
  90.     // These strings are declared to store the parsed data. There should be one variable per column of data
  91.     string input,firstName, lastName, eyeColor, height, weight, DOB;
  92.     // Specify the delimeter for parsing
  93.     string delimeter = ",";
  94.     int heightValue;
  95.     float weightValue;
  96.  
  97.     // If the file is readable
  98.     if (file.is_open())
  99.     {
  100.         // Keep reading until the end of line.
  101.         while (getline(file, input, '\n'))
  102.         {
  103.             // Read the input string until the delimeter is found.
  104.             firstName = input.substr(0, input.find(delimeter));
  105.             input.erase(0, input.find(delimeter) + delimeter.length());
  106.             // Erase the parsed input so that it is not read again
  107.  
  108.             lastName = input.substr(0, input.find(delimeter));
  109.             input.erase(0, input.find(delimeter) + delimeter.length());
  110.  
  111.             eyeColor = input.substr(0, input.find(delimeter));
  112.             input.erase(0, input.find(delimeter) + delimeter.length());
  113.  
  114.             height = input.substr(0, input.find(delimeter));
  115.             input.erase(0, input.find(delimeter) + delimeter.length());
  116.  
  117.             weight= input.substr(0, input.find(delimeter));
  118.             input.erase(0, input.find(delimeter) + delimeter.length());
  119.  
  120.             DOB = input.substr(0, input.find(delimeter));
  121.             input.erase(0, input.find(delimeter) + delimeter.length());
  122.  
  123.             // Convert string to an integer
  124.             heightValue = stoi(height);
  125.             // Convert string to a float
  126.             weightValue = stof(weight);
  127.  
  128.             cout << firstName << ' ' << lastName << endl;
  129.             cout << "Eye color: " << eyeColor << endl;
  130.             cout << "Height in inches: " << heightValue << endl;
  131.             cout << "Weight in lbs: " << weightValue << endl;
  132.             cout << "Date of Birth (DD/MM/YYYY): " << DOB << "\n" << endl;
  133.  
  134.         }
  135.         // Always close a file when done reading.
  136.         file.close();
  137.     }
  138.     else
  139.         cout << "Unable to open file" << endl;
  140.     // Consumes extra newline character at end
  141.     cin.ignore();
  142. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement