daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Sep 20th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream> // for: std::cout and std::cerr
  2. #include <fstream>  // for: std::ifstream
  3. #include <sstream>  // for: std::stringstream
  4. #include <string>   // for: std::string, std::getline()
  5. #include <iomanip>
  6.  
  7.  
  8. // IMPORTANT: Generally each comment line corresponds to one line of code
  9. // The exception from this rule are comments enclosed in < > brackets, those are just for information
  10.  
  11. // function that converts a text data file to json format
  12. std::string convert_json(const std::string& filename, std::ostream& output=std::cout)
  13.     {
  14.  
  15.     // declare and initialize an input file stream with the given file name
  16.     std::ifstream infile (filename);
  17.     //std::ofstream outfile("output.txt");
  18.  
  19.         // check if the file was opened, if not return FAILURE
  20.         if (infile.is_open()) {
  21.  
  22.             // declare a string variable that will hold a single line of input data read from the file
  23.             std::string line;
  24.  
  25.             // write the json opening braces to the output stream
  26.             output << "{\n";
  27.             // < main input processing loop should start here >
  28.  
  29.                 /* //Output the whole input file!
  30.                 while (getline(infile, line)) {
  31.                     std::cout << line << std::endl;
  32.                 }
  33.                 */
  34.  
  35.             // while it's possible, read a single line from the input file
  36.             while (getline(infile, line))
  37.             {
  38.  
  39.                 // declare an input string stream and initialize it with the line read from file (wrap the input line into a istringstream)
  40.                 std::istringstream ss;
  41.                 ss.str(line);
  42.  
  43.                 //declare string variables that will hold the tag and another one that will hold the equality symbol read from the input line
  44.                 std::string tag;
  45.                 std::string equality_symbol;
  46.  
  47.                 // read the tag from the string stream into the tag variable
  48.                 ss >> tag;
  49.  
  50.                 //check if the tag begins with the symbol #
  51.                 // < if it begins with # we are dealing with a comment >
  52.                 //<remember that a comment must be marked with a tag "_comment" in the output
  53.                 if (tag.find('#') == 0)
  54.                 {
  55.                     //if it's a comment just write the whole line to the output
  56.                     std::cout << "_comment " << line.substr(1, line.length());  //quoted
  57.                 }
  58.  
  59.                 // if the tag doesn't begin with the # symbol, try to read the "=" separator symbol from the input stream into the appropriate variable
  60.                 // check if the read separator symbol is equal to '='
  61.  
  62.                 else (tag.find('=') == 0);
  63.                 {
  64.                     // < if it's equal to '=' we are dealing with a correctly formatted data line (or so it seems) >
  65.                     std::cout << ":";
  66.  
  67.                     // if '=' was read we need to process the data words (remember there may be more than one word)
  68.                     // declare a string variable that will hold a single word
  69.                     std::string word;
  70.  
  71.                     // while it's possible to  read a single word from the string stream into this variable:
  72.                     // write a single json line to the output using the correct tag and the word just read
  73.                 }
  74.  
  75.  
  76.  
  77.                 // send the closing json braces to the output
  78.  
  79.                 infile.close();                    //input file closed
  80.                 output << line;                   //output
  81.             }
  82.         }
  83.         else std::cout << "Unable to open file";
  84.  
  85.     // return
  86.     return 0;
  87. }
  88.  
  89.  
  90. // main function, notice it doesn't have an explicit return statement
  91. int main()
  92. {
  93.     std::cout << "***** Hello Saxion! *****\n";
  94.     std::string result;
  95.     // prompt a user to enter the file name to process
  96.     std::cout << "Please enter a name for a filename.\n" << std::endl;
  97.     // declare a string variable to hold the file name
  98.     std::string filename;
  99.     // read the file name from the standard input
  100.     std::cin >> filename;
  101.     // call the conversion function and print the value returned by it to the std::cout
  102.     result = convert_json(filename);
  103.  
  104.  
  105. // < note that the conversion function sends output to the output stream internally >
  106. // < here you have to print only the result returned by this function >
  107.  
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top