Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>//part of the question is about function signature .. the types of parameters //below we have completely described the type again //slight improvement of parameter type .. not recommended //I hope I had not said too much and too little at the same time //3 possible improvements follow void buildlist1( char (&amp;da)[5][25], std::ifstream &amp; ifs) { for (int i = 0; i &lt; 5; ++i)//not recommended explicit magic number { da[i][0] = 0;//compensate for empty file/stream ifs &gt;&gt; da[i];//dangerous cout &lt;&lt; da[i] &lt;&lt; "...";//debugging danger data can be lost with data larger than allocated } } //cleaner parameter type struct maintaininfo { //consider this struct as possible data to pass as reference or value static const int x1 = 5; static const int x2 = 25; //the array info is available char data[x1][x2]; }; void buildlist2( maintaininfo &amp; mi, std::ifstream &amp; ifs) { for (int i = 0; i &lt; maintaininfo::x1; ++i)//number defined in struct/class { mi.data[i][0] = 0;//compensate for empty file/stream ifs &gt;&gt; mi.data[i];//dangerous overflow possibly cout &lt;&lt; mi.data[i] &lt;&lt; "...";//debugging danger data can be lost with data larger than allocated } //elided similar to above } // IMHO I would prefer something in this direction // The person posing question may have contrary priorities and constraints void buildlistmodern( std::vector&lt;string&gt; &amp; svs, std::ifstream &amp; ifs) { for (int i = 0; i &lt; 5; ++i)//magic explicit number { std::string s;//compensate for empty file ifs &gt;&gt; s; //possibly process the string s here again svs.push_back(s); cout &lt;&lt; svs[i] &lt;&lt; "..."; } } int readfile() { const int listsize = 5; auto filename = "c:\\delete\\delete.txt"; { cout &lt;&lt; endl &lt;&lt; "this seems unsafe and old fashioned arrays for casual use are dangerous" &lt;&lt; endl ; std::ifstream ifs(filename); if (ifs.good()) { char cardlist[listsize][25];//dangerous magic explicit numbers buildlist1(cardlist, ifs); cout &lt;&lt; endl &lt;&lt; "final tally" &lt;&lt; endl; for (int i = 0; i &lt; 5; ++i) { cout &lt;&lt; cardlist[i] &lt;&lt; "..."; } } else cout &lt;&lt; "File Problem" &lt;&lt; endl; } { cout &lt;&lt; endl &lt;&lt; "array is encapsulated within type" &lt;&lt; endl ; std::ifstream ifs(filename); if (ifs.good()) { maintaininfo cardlist; buildlist2(cardlist, ifs); cout &lt;&lt; endl &lt;&lt; "final tally" &lt;&lt; endl; for (int i = 0; i &lt; 5; ++i) { cout &lt;&lt; cardlist.data[i] &lt;&lt; "..."; } } else cout &lt;&lt; "File Problem" &lt;&lt; endl; } { cout &lt;&lt; endl &lt;&lt; "this looks more modern ... but may be beyond the scope of the question" &lt;&lt; endl; std::ifstream ifs(filename); if (ifs.good()) { std::vector&lt;string&gt;svs; buildlistmodern(svs, ifs); cout &lt;&lt; endl &lt;&lt; "final tally "&lt;&lt; endl; if (svs.size() == 0) cout &lt;&lt; "No data in file" &lt;&lt; endl; for (const auto &amp; s : svs) { cout &lt;&lt; s &lt;&lt; "..."; } cout &lt;&lt; endl &lt;&lt; "fixed method ... not recommended ...but you might know better" &lt;&lt; endl; for (int i = 0; i &lt; 5; ++i) { cout &lt;&lt; svs[i] &lt;&lt; "..."; } } else cout &lt;&lt; "File Problem" &lt;&lt; endl; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload