{"id":2827,"date":"2014-02-21T17:24:09","date_gmt":"2014-02-21T17:24:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/21\/how-to-copy-a-txt-file-to-a-char-array-in-c-collection-of-common-programming-errors\/"},"modified":"2014-02-21T17:24:09","modified_gmt":"2014-02-21T17:24:09","slug":"how-to-copy-a-txt-file-to-a-char-array-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/02\/21\/how-to-copy-a-txt-file-to-a-char-array-in-c-collection-of-common-programming-errors\/","title":{"rendered":"How to copy a .txt file to a char array in c++-Collection of common programming errors"},"content":{"rendered":"<p>With<\/p>\n<pre><code>myfile &gt;&gt; myArray[i]; \n<\/code><\/pre>\n<p>you are reading file word by word which causes skipping of the spaces.<\/p>\n<p>You can read entire file into the string with<\/p>\n<pre><code>std::ifstream in(\"FileReadExample.cpp\");\nstd::string contents((std::istreambuf_iterator(in)), \n    std::istreambuf_iterator());\n<\/code><\/pre>\n<p>And then you can use <code>contents.c_str()<\/code> to get char array.<\/p>\n<p><strong>How this works<\/strong><\/p>\n<p><code>std::string<\/code> has range constructor that copies the sequence of characters in the range [first,last) <strong>note that it will not copy last<\/strong>, in the same order:<\/p>\n<pre><code>template \n  string  (InputIterator first, InputIterator last);\n<\/code><\/pre>\n<p><code>std::istreambuf_iterator<\/code> iterator is input iterator that read successive elements from a stream buffer.<\/p>\n<pre><code>std::istreambuf_iterator(in)\n<\/code><\/pre>\n<p>will create iterator for our <code>ifstream in<\/code> (beginning of the file), and if you don&#8217;t pass any parameters to the constructor, it will create end-of-stream iterator (last position):<\/p>\n<blockquote>\n<p>The default-constructed std::istreambuf_iterator is known as the end-of-stream iterator. When a valid std::istreambuf_iterator reaches the end of the underlying stream, it becomes equal to the end-of-stream iterator. Dereferencing or incrementing it further invokes undefined behavior.<\/p>\n<\/blockquote>\n<p>So, this will copy all characters, starting from the first in the file, until the next character is end of the stream.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With myfile &gt;&gt; myArray[i]; you are reading file word by word which causes skipping of the spaces. You can read entire file into the string with std::ifstream in(&#8220;FileReadExample.cpp&#8221;); std::string contents((std::istreambuf_iterator(in)), std::istreambuf_iterator()); And then you can use contents.c_str() to get char array. How this works std::string has range constructor that copies the sequence of characters in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2827","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2827"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2827\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}