{"id":1020,"date":"2017-03-22T09:06:44","date_gmt":"2017-03-22T16:06:44","guid":{"rendered":"http:\/\/somethingk.com\/main\/?p=1020"},"modified":"2017-03-23T07:39:55","modified_gmt":"2017-03-23T14:39:55","slug":"reading-a-file-with-c","status":"publish","type":"post","link":"https:\/\/somethingk.com\/main\/reading-a-file-with-c\/","title":{"rendered":"Reading a File with C++"},"content":{"rendered":"<section id=\"text-4\" class=\"widget boka-widget widget_text amr_widget\">\t\t\t<div class=\"textwidget\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block; text-align:center;\"\r\n     data-ad-layout=\"in-article\"\r\n     data-ad-format=\"fluid\"\r\n     data-ad-client=\"ca-pub-7619916617995509\"\r\n     data-ad-slot=\"9102150708\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script><\/div>\n\t\t<\/section>\n<p>Here is just a simple example of reading a file with iostream. Just follow my comments to understand.<\/p>\n<p>Good luck, sometimes newlines and spaces can mess you up, you may need to utilize an <a href=\"http:\/\/www.cplusplus.com\/reference\/istream\/istream\/ignore\/\">ignore()<\/a> or something else.<\/p>\n<div class=\"snippetcpt-wrap\" id=\"snippet-1022\" data-id=\"1022\" data-edit=\"\" data-copy=\"\/main\/wp-json\/wp\/v2\/posts\/1020?snippet=17b1fac833&#038;id=1022\" data-fullscreen=\"https:\/\/somethingk.com\/main\/code-snippets\/reading-a-file-with-c\/?full-screen=1\">\n\t\t\t\t<pre class=\"prettyprint linenums lang-c_cpp\" title=\"Reading a File with C++\">#include &lt;fstream&gt;\r\n#include &lt;iostream&gt; \r\nusing namespace std;\r\n\r\nvoid evaluateFile(string ins); \/\/Do something with the file contents\r\nvoid readFile(); \/\/Read the file\r\n\r\nint main(){\r\n    readFile();\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\nvoid readFile(){\r\n    ifstream input;\r\n    string fileName;\r\n    cout &lt;&lt; &quot;What is the full path of file to be read&quot; &lt;&lt; endl;\r\n    getline(cin, fileName); \/\/Prompt user for filename, needs to be the fill path\r\n    input.open(fileName); \/\/Open thhe file\r\n    if (input.fail()){ \/\/Fail case\r\n        cout &lt;&lt; &quot;Sorry, cannot open file. Quitting now.&quot; &lt;&lt; endl;\r\n        exit(0);\r\n    }\r\n    while(!input.eof()){ \/\/Read file until the end of file is reached\r\n        string line;\r\n        getline(input, line);\r\n        evaluateFile(line);\r\n    }\r\n    input.close();\r\n}\r\n\r\nvoid evaluateFile(string line){\r\n    cout &lt;&lt; line &lt;&lt; endl;\r\n    \/\/DO SOMETHING WITH THE FILE INPUT\r\n}<\/pre>\n\t\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is just a simple example of reading a file with iostream. Just follow my comments to understand. Good luck, sometimes newlines and spaces can mess you up, you may need to utilize an ignore() or something else.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[177],"tags":[303,531,529,530,532],"class_list":["post-1020","post","type-post","status-publish","format-standard","hentry","category-development","tag-c","tag-file","tag-fstream","tag-iostream","tag-read"],"_links":{"self":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/comments?post=1020"}],"version-history":[{"count":4,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"predecessor-version":[{"id":1067,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/posts\/1020\/revisions\/1067"}],"wp:attachment":[{"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingk.com\/main\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}