Am newbie in using WinAPI in C++. Just searched the internet for similar issues but unable to find correct one.
Am trying to read a file, which includes integer values and to save the values to a string Buffer. Then I need to convert the string to int array in order to calculate average values of all array elements.
At this moment I experience a problem, that values from a file are not being saved correctly - there is redundantion of some values at output.
As example, the input file looks like this: 1  2  5  3  96  72  33  47  43  91
The code is throwing values like: 1  2  5   3  96   72  33   47  43   91  43   91  43   91  43   91  43
Please see my code:
#include <iostream>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main( int argc, TCHAR *argv[] )
{
HANDLE hfile = CreateFile("RandomValues.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); 
    DWORD dwBytesRead = 0;
    int SizeofFile=GetFileSize(hfile,NULL);
    LPSTR Buffer=NULL;
    DWORD dwSizeofBuffer = sizeof(Buffer);
    Buffer =( LPSTR ) GlobalAlloc( GPTR, MAX_PATH * sizeof(CHAR) );
    cout<<SizeofFile<<endl;
    for(unsigned int i=0;i<dwSizeofBuffer;i++)
    {
        ReadFile( hfile, Buffer, dwSizeofBuffer, &dwBytesRead, NULL );
        Buffer[i] = (wchar_t)Buffer[i];
        cout<<Buffer<<" ";  //checking, if values are read correct to Buffer
        
    }
    GlobalFree(Buffer);
    CloseHandle(hfile); 
    return 0;
}
UPDATE: I was asked to put the latest code - it's simpler (I think) and uses vector array and stoi function, although the input looks like: 11111. One user suggested to paste the new code here as edit:
#include <iostream>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>
using namespace std;
int main( int argc, TCHAR *argv[] )
{
    //proces 2 uzyskuje dostep do plilu z liczbami i oblicza srednia liczb a wynik podaje na ekranie
    fstream File;
    File.open("RandomValues.txt", ios::in | ::ios::out);
    string Buffer = " ";
    if( File.good() ){
        for(int i=0;!File.eof();i++)
    {
        getline(File,Buffer);
        cout<<Buffer;
    }
    }
    
    
    //int str_length = Buffer.length(); 
    //int* Values;
    //Values = new int[str_length];
    vector <int> Values; 
    int j = 0;
    for (int i = 0; Buffer[i] != '\0'; i++) { 
  
         
         if (Buffer[i] == ' '){ 
            // Increment j to point to next 
            // array location 
            j++; 
        } 
        else { 
  
            // subtract str[i] by 48 to convert it to int 
            // Generate number by multiplying 10 and adding 
            // (int)(str[i]) 
           // Values[j] = Values[j] * 10 + (Buffer[i] - 48); //tries as well as *(Values + j) = *(Values + j) * 10 + (*(Buffer + i) -48) and with stoi(Buffer)
           Values.push_back(stoi(Buffer)); 
        } 
    } 
  
    for (unsigned int i = 0; i <= Values.size(); i++) { 
        cout << Values[i] << " "; 
         
    } 
//  delete [] Values;
    File.close();
    return 0;
}
 
    