Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Parsing a line out of a large file
    primarykey
    data
    text
    <p>I have read an entire file into a string from a memory mapped file Win API </p> <pre><code>CreateFile( "WarandPeace.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ) </code></pre> <p>etc...</p> <p>Each line is terminated with a CRLF. I need to find something on a line like "Spam" in the line "I love Spam and Eggs" (and return the entire line (without the CRLF) in a string (or a pointer to the location in the string) The original string cannot be altered.</p> <p><strong>EDITED:</strong></p> <p>Something like this:</p> <pre><code>string ParseStr( string sIn, string sDelim, int nField ) { int match, LenStr, LenDelim, ePos, sPos(0), count(0); string sRet; LenDelim = sDelim.length(); LenStr = sIn.length(); if( LenStr &lt; 1 || LenDelim &lt; 1 ) return ""; // Empty String if( nField &lt; 1 ) return ""; //=========== cout &lt;&lt; "LenDelim=" &lt;&lt; LenDelim &lt;&lt; ", sIn.length=" &lt;&lt; sIn.length() &lt;&lt; endl; for( ePos=0; ePos &lt; LenStr; ePos++ ) // iterate through the string { // cout &lt;&lt; "sPos=" &lt;&lt; sPos &lt;&lt; ", LenStr=" &lt;&lt; LenStr &lt;&lt; ", ePos=" &lt;&lt; ePos &lt;&lt; ", sIn[ePos]=" &lt;&lt; sIn[ePos] &lt;&lt; endl; match = 1; // default = match found for( int k=0; k &lt; LenDelim; k++ ) // Byte value { if( ePos+k &gt; LenStr ) // end of the string break; else if( sIn[ePos+k] != sDelim[k] ){ // match failed match = 0; break; } } //=========== if( match || (ePos == LenStr-1) ) // process line { if( !match ) ePos = LenStr + LenDelim; // (ePos == LenStr-1) count++; // cout &lt;&lt; "sPos=" &lt;&lt; sPos &lt;&lt; ", ePos=" &lt;&lt; ePos &lt;&lt; " &gt;" &lt;&lt; sIn.substr(sPos, ePos-sPos) &lt;&lt; endl; if( count == nField ){ sRet = sIn.substr(sPos, ePos-sPos); break; } ePos = ePos+LenDelim-1; // jump over Delim sPos = ePos+1; // Begin after Delim } // cout &lt;&lt; "Final ePos=" &lt;&lt; ePos &lt;&lt; ", count=" &lt;&lt; count &lt;&lt; ", LenStr=" &lt;&lt; LenStr &lt;&lt; endl; }// next return sRet; } </code></pre> <p>If you like it, vote it up. If not, let's see what you got.</p>
    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.
 

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