Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here you go (don't forget to read the comments):</p> <p><strong>Example:</strong></p> <pre><code>void Example( void ) { // DECLARATION // *Declare iFile as std::ifstream and attempt to open file: Example.txt std::ifstream iFile( "Example.txt" ); // *If iFile is open, do this: if( iFile.is_open( ) ) { // DECLARATION // *You could declare strLine as an array of char if you want std::string strLine = ""; unsigned int nLineCount = 0; // DO WHATEVER // *Read iFile line by line using std::getline while( std::getline( iFile, strLine ) ) { // *For the line after every 6th line, we shall print // as a title // *( nLineCount % 6 ) gives us the remainder of // nLineCount / 6 and if the remainder is 0, then // do this: if( !( nLineCount % 6 ) ) { std::cout &lt;&lt; "Title = " &lt;&lt; strLine &lt;&lt; std::endl; } // *For every other line, we shall print it normally else { std::cout &lt;&lt; strLine &lt;&lt; std::endl; } // *Increase nLineCount by 1; nLineCount ++; } // CLEAN-UP // *Done using inFile - so close it inFile.close( ); } }; </code></pre> <p><strong>Tested:</strong></p> <pre><code>Title = 1 2 3 4 5 6 Title = 7 8 9 10 11 12 Title = 13 ... Debugging &gt; Paused! Enter any key to continue... </code></pre> <p><strong>More...</strong> Without comments:</p> <pre><code>void Example( void ) { std::ifstream iFile( "Example.txt" ); if( iFile.is_open( ) ) { std::string strLine = ""; unsigned int nLineCount = 0; while( std::getline( iFile, strLine ) ) { if( !( nLineCount % 6 ) ) std::cout &lt;&lt; "Title = " &lt;&lt; strLine &lt;&lt; std::endl; else std::cout &lt;&lt; strLine &lt;&lt; std::endl; nLineCount ++; } iFile.close( ); } }; </code></pre> <p>For-loop method, short and clean:</p> <pre><code>void Example( void ) { std::ifstream iFile( "Example.txt" ); if( iFile.is_open( ) ) { std::string strLine = ""; for( unsigned int nLineCount = 0; std::getline( iFile, strLine ); nLineCount ++ ) { if( !( nLineCount % 6 ) ) std::cout &lt;&lt; "Title = " &lt;&lt; strLine &lt;&lt; std::endl; else std::cout &lt;&lt; strLine &lt;&lt; std::endl; } iFile.close( ); } }; </code></pre>
 

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