簡體   English   中英

C++ WinAPI - 從文件中讀取和轉換值

[英]C++ WinAPI - reading and converting values from a file

我是在 C++ 中使用 WinAPI 的新手。 剛剛在互聯網上搜索了類似的問題,但找不到正確的問題。

我正在嘗試讀取一個文件,其中包括 integer 值並將這些值保存到字符串緩沖區。 然后我需要將字符串轉換為 int 數組以計算所有數組元素的平均值。

此時我遇到了一個問題,即文件中的值未正確保存 - output 中的某些值存在冗余。

例如,輸入文件如下所示: 1 2 5 3 96 72 33 47 43 91代碼拋出的值如下: 1 2 5 3 96 72 33 47 43 91 43 91 43 91 43 91 43

請看我的代碼:

#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;
}

更新:我被要求輸入最新的代碼 - 它更簡單(我認為)並使用向量數組和 stoi function,盡管輸入看起來像:11111。一位用戶建議在此處粘貼新代碼作為編輯:

#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;
}

以下是如何將文件中的整數讀入向量。 我假設整數僅由空格分隔(即文件中沒有逗號或類似字符)。 如果這不是真的,那么代碼必須稍微復雜一些(但不多)。

// open the file
ifstream File("RandomValues.txt", ios::in);
if (!File.is_open())
    cerr << "failed to open file\n";

vector<int> Values;
// read the values (three lines of code !!!)
int val;
while (File >> val)
    Values.push_back(val);

// print out the values
for (unsigned int i = 0; i < Values.size(); i++) { 
    cout << Values[i] << " ";
cout << '\n';

有人建議我使用標准的fstream方法,當然我有它並且它可以正確讀取文件內容,問題是在將值從字符串轉換為 int 數組時開始的。 我把完整的代碼放在這里比在 pastebin 上看得更好:

#include <iostream>
#include <Windows.h>
#include <string>
#include <tchar.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
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 = " ";
    for(int i=0;!File.eof();i++)
    {
        getline(File,Buffer);
        cout<<Buffer;
    }
    
    int str_length = Buffer.length(); 
    int* Values = new int(str_length);
    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)
        } 
    } 
  
    for (unsigned int i = 0; i <= sizeof(Values); i++) { 
        cout << Values[i] << " "; 
         
    } 
    delete [] Values;
    File.close();
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM