C++ WinAPI - 从文件中读取和转换值

wiki11ful

我是在 C++ 中使用 WinAPI 的新手。刚刚在互联网上搜索了类似的问题,但找不到正确的问题。

我正在尝试读取包含整数值的文件并将这些值保存到字符串缓冲区。然后我需要将字符串转换为 int 数组以计算所有数组元素的平均值。

此时我遇到一个问题,文件中的值没有正确保存 - 输出时某些值存在冗余。

例如,输入文件如下所示: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 函数,尽管输入看起来像: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';

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章