Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> // for: std::cout and std::cerr
- #include <fstream> // for: std::ifstream
- #include <sstream> // for: std::stringstream
- #include <string> // for: std::string, std::getline()
- #include <iomanip>
- // IMPORTANT: Generally each comment line corresponds to one line of code
- // The exception from this rule are comments enclosed in < > brackets, those are just for information
- // function that converts a text data file to json format
- std::string convert_json(const std::string& filename, std::ostream& output=std::cout)
- {
- // declare and initialize an input file stream with the given file name
- std::ifstream infile (filename);
- //std::ofstream outfile("output.txt");
- // check if the file was opened, if not return FAILURE
- if (infile.is_open()) {
- // declare a string variable that will hold a single line of input data read from the file
- std::string line;
- // write the json opening braces to the output stream
- output << "{\n";
- // < main input processing loop should start here >
- /* //Output the whole input file!
- while (getline(infile, line)) {
- std::cout << line << std::endl;
- }
- */
- // while it's possible, read a single line from the input file
- while (getline(infile, line))
- {
- // declare an input string stream and initialize it with the line read from file (wrap the input line into a istringstream)
- std::istringstream ss;
- ss.str(line);
- //declare string variables that will hold the tag and another one that will hold the equality symbol read from the input line
- std::string tag;
- std::string equality_symbol;
- // read the tag from the string stream into the tag variable
- ss >> tag;
- //check if the tag begins with the symbol #
- // < if it begins with # we are dealing with a comment >
- //<remember that a comment must be marked with a tag "_comment" in the output
- if (tag.find('#') == 0)
- {
- //if it's a comment just write the whole line to the output
- std::cout << "_comment " << line.substr(1, line.length()); //quoted
- }
- // if the tag doesn't begin with the # symbol, try to read the "=" separator symbol from the input stream into the appropriate variable
- // check if the read separator symbol is equal to '='
- else (tag.find('=') == 0);
- {
- // < if it's equal to '=' we are dealing with a correctly formatted data line (or so it seems) >
- std::cout << ":";
- // if '=' was read we need to process the data words (remember there may be more than one word)
- // declare a string variable that will hold a single word
- std::string word;
- // while it's possible to read a single word from the string stream into this variable:
- // write a single json line to the output using the correct tag and the word just read
- }
- // send the closing json braces to the output
- infile.close(); //input file closed
- output << line; //output
- }
- }
- else std::cout << "Unable to open file";
- // return
- return 0;
- }
- // main function, notice it doesn't have an explicit return statement
- int main()
- {
- std::cout << "***** Hello Saxion! *****\n";
- std::string result;
- // prompt a user to enter the file name to process
- std::cout << "Please enter a name for a filename.\n" << std::endl;
- // declare a string variable to hold the file name
- std::string filename;
- // read the file name from the standard input
- std::cin >> filename;
- // call the conversion function and print the value returned by it to the std::cout
- result = convert_json(filename);
- // < note that the conversion function sends output to the output stream internally >
- // < here you have to print only the result returned by this function >
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement