{"id":31678,"date":"2023-01-23T10:57:18","date_gmt":"2023-01-23T05:27:18","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/"},"modified":"2023-01-23T10:57:18","modified_gmt":"2023-01-23T05:27:18","slug":"solved-data-file-handling-find-count-of-word-in-file","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/","title":{"rendered":"[Solved] Data File Handling &#8211; find count of word in file"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41596818\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41596818\" data-parentid=\"41595762\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>The algorithm follows from what you have. Structure your while-loop like this:<\/p>\n<pre><code>while(!fin.eof()) {\n    bool found = false;    \n    do {\n        fin.get(ch);\n        found = found || ch == 'd';\n    } while (ch == 'd' &amp;&amp; fin);\n\n    if (found &amp;&amp; ch == 'o') {\n        \/\/ boom goes the dynamite\n    }\n}\n<\/code><\/pre>\n<p>The purpose of the <code>do-while<\/code> is to eliminate repeating <code>d<\/code>&#8216;s, so that after that loop, you simply check if the next character is <code>o<\/code>.<\/p>\n<h2>Note<\/h2>\n<ul>\n<li>In terms of typing, the type for <code>ch<\/code> should be <code>char ch<\/code><\/li>\n<\/ul>\n<h2>Explained<\/h2>\n<ul>\n<li><code>while(!fin.eof())<\/code>\n<ul>\n<li>Repeat the next few lines until we reach the end of the file<\/li>\n<\/ul>\n<\/li>\n<li><code>do {<\/code>\n<ul>\n<li>Beginning of a <code>do-while<\/code> loop<\/li>\n<\/ul>\n<\/li>\n<li><code>fin.get(ch);<\/code>\n<ul>\n<li>Read a single byte (character) from the file<\/li>\n<\/ul>\n<\/li>\n<li><code>found = found || ch == 'd';<\/code>\n<ul>\n<li>Set <code>found<\/code> to true if we have already found a <code>d<\/code> or the current character is a <code>d<\/code><\/li>\n<\/ul>\n<\/li>\n<li><code>} while (ch == 'd' &amp;&amp; fin);<\/code>\n<ul>\n<li>End of <code>do-while<\/code>. Repeat the loop until the last character read is not a <code>d<\/code> or we have reached the end of the file<\/li>\n<\/ul>\n<\/li>\n<li><code>if (found &amp;&amp; ch == 'o') {<\/code>\n<ul>\n<li>If we were able to satisfy the condition for setting <code>found<\/code> to true and the last character we read is <code>o<\/code>&#8230;<\/li>\n<\/ul>\n<\/li>\n<li><code>\/\/ boom goes the dynamite<\/code>\n<ul>\n<li>then we have successfully found the word <code>do<\/code> <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>Sans <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.cppreference.com\/w\/cpp\/io\/basic_ios\/eof\"><code>std::ios::eof<\/code><\/a><\/h2>\n<p>I won&#8217;t explain this next bit, but it will follow closely with what I already posted. The goal here is to protect yourself from reading an already empty file.<\/p>\n<pre><code>while(fin &gt;&gt; ch) {\n    while(ch == 'd' &amp;&amp; fin.get(ch)) {    \n        if (ch != 'd') {\n            if (ch != 'o') {\n                break;\n            }\n            \/\/ Found it!\n        }\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">8<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Data File Handling &#8211; find count of word in file <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The algorithm follows from what you have. Structure your while-loop like this: while(!fin.eof()) { bool found = false; do { fin.get(ch); found = found || ch == &#8216;d&#8217;; } while (ch == &#8216;d&#8217; &amp;&amp; fin); if (found &amp;&amp; ch == &#8216;o&#8217;) { \/\/ boom goes the dynamite } } The purpose of the do-while &#8230; <a title=\"[Solved] Data File Handling &#8211; find count of word in file\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\" aria-label=\"More on [Solved] Data File Handling &#8211; find count of word in file\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,513,820],"class_list":["post-31678","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-c11","tag-data-files"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Data File Handling - find count of word in file - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Data File Handling - find count of word in file - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The algorithm follows from what you have. Structure your while-loop like this: while(!fin.eof()) { bool found = false; do { fin.get(ch); found = found || ch == &#039;d&#039;; } while (ch == &#039;d&#039; &amp;&amp; fin); if (found &amp;&amp; ch == &#039;o&#039;) { \/\/ boom goes the dynamite } } The purpose of the do-while ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-23T05:27:18+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Data File Handling &#8211; find count of word in file\",\"datePublished\":\"2023-01-23T05:27:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\"},\"wordCount\":188,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"c++11\",\"data-files\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\",\"name\":\"[Solved] Data File Handling - find count of word in file - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-23T05:27:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Data File Handling &#8211; find count of word in file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Data File Handling - find count of word in file - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Data File Handling - find count of word in file - JassWeb","og_description":"[ad_1] The algorithm follows from what you have. Structure your while-loop like this: while(!fin.eof()) { bool found = false; do { fin.get(ch); found = found || ch == 'd'; } while (ch == 'd' &amp;&amp; fin); if (found &amp;&amp; ch == 'o') { \/\/ boom goes the dynamite } } The purpose of the do-while ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/","og_site_name":"JassWeb","article_published_time":"2023-01-23T05:27:18+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Data File Handling &#8211; find count of word in file","datePublished":"2023-01-23T05:27:18+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/"},"wordCount":188,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","c++11","data-files"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/","url":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/","name":"[Solved] Data File Handling - find count of word in file - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-23T05:27:18+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-data-file-handling-find-count-of-word-in-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Data File Handling &#8211; find count of word in file"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/31678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=31678"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/31678\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=31678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=31678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=31678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}