title: Process a simple text file in parallels using OpenMP
body: <p>I have a task - I need to write an OpenMP program which should do:</p>
  <ol>
  <li>Open text file in 2 and then 4 threads.</li>
  <li>Do some basic processing like count letters, punctuation symbols, words, and write out it in console.</li>
  <li>I MUST use OpenMP loops.</li>
  </ol>
  <p>UPD: I re-re-write whole thing from the scratch, and added &quot;tmpresult&quot; variable which is counting chars in each chunk of chars, as they are being processed. Since &quot;tmpresult&quot; (Total symbols in file) is always equal file length, i assume that inconsistency in counting letters is not result of some symbols being cut off when i breaking file into chunks. Because total file length is equal total symbols count. But then what can cause those inconsistencies?</p>
  <p>Output examples:</p>
  <pre><code>Total threads used 2 
  Total symbols in file TinyText2.txt = 9075 
  Total symbols without white spaces = 7905 
  Total punctuation symbols = 156 
  
  Total threads used 4 
  Total symbols in file TinyText2.txt = 9075 
  Total symbols without white spaces = 7905 
  Total punctuation symbols = 155 
  
  Total threads used 6 
  Total symbols in file TinyText2.txt = 9075 
  Total symbols without white spaces = 7905 
  Total punctuation symbols = 155 
  
  Total threads used 8 
  Total symbols in file TinyText2.txt = 9075 
  Total symbols without white spaces = 7904 
  Total punctuation symbols = 155
  </code></pre>
  <p>Code of my program:</p>
  <pre class="lang-cpp prettyprint-override"><code>void* LettersCounter(const string inFileName, int threads) {
  
      ifstream pFile(inFileName);
      string s = inFileName;
      int numThreads = threads;
      long charsCounter = 0;
      long punctCounter = 0;
      pFile.seekg(0, ios_base::end);
      long length = pFile.tellg();
      pFile.seekg(0, ios_base::beg);
      long lettersCount[numThreads];
      long punctCount[numThreads];
      long chunkSize = length / numThreads;
      char buffer[numThreads][chunkSize];
      long tmp[numThreads];
      long tmpresult = 0;
      for (int z = 0; z &lt;numThreads; z++){
          tmp[z] = 0;
          lettersCount[z]=0;
          punctCount[z]=0;
      }
  
      #pragma omp parallel for shared(charsCounter, lettersCount, punctCount) private (pFile)
  
      for (int j = 0; j &lt; numThreads; j++) {
          int currentThread = omp_get_thread_num();
          if (currentThread &lt; numThreads-1) {
          ifstream pFile(s);
          pFile.seekg (currentThread*chunkSize , ios_base::beg);
          pFile.read(buffer[j], chunkSize);
          pFile.seekg (0 , ios_base::beg);
          pFile.close();
          for (int i = 0; i &lt; chunkSize; i++) {
  
                  if (isgraph(buffer[j][i])) {
                      lettersCount[j]++;
                  }
  
                  if (buffer[j][i] != 0) {
                      tmp[j]++;
                                  }
                  if (ispunct(buffer[j][i])) {
                      punctCount[j]++;
                  }
      }} else {
  
      ifstream pFile(s);
      pFile.seekg (currentThread*chunkSize , ios_base::beg);
      long curPosition = pFile.tellg();
      pFile.seekg (0 , ios_base::end);
      long fileLenght = pFile.tellg();
      long finalChunkSize = fileLenght - curPosition;
      pFile.seekg (curPosition , ios_base::beg);
      char finalBuffer[finalChunkSize];
      pFile.read(finalBuffer, finalChunkSize);
      pFile.seekg (0 , ios_base::beg);
      pFile.close();
      for (int i = 0; i &lt; finalChunkSize; i++) {
  
                  if (isgraph(finalBuffer[i])) {
                      lettersCount[j]++;
                  }
  
                  if (finalBuffer[i] != 0) {
                      tmp[j]++;
                                  }
  
                  if (ispunct(finalBuffer[i])) {
                      punctCount[j]++;
                  }
              }
  
      }}
  
      for (int z = 0; z &lt;numThreads; z++){
  
              charsCounter = charsCounter + lettersCount[z];
              punctCounter = punctCounter + punctCount[z];
              tmpresult = tmpresult + tmp[z];
          }
      printf(&quot;Total threads used %li \n&quot;, threads);
      printf(&quot;Total symbols in file %s = %li \n&quot;,s.c_str(), tmpresult);
      printf(&quot;Total symbols without white spaces = %li \n&quot;, charsCounter);
      printf(&quot;Total punctuation symbols = %li \n&quot;, punctCounter);
      pFile.close();
  }
  }
  
  int main() {LettersCounter(&quot;TinyText2.txt&quot;, 4);}
  </code></pre>