c++ - C++ WinAPI - 从文件中读取和转换值
问题描述
我是在 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';
推荐阅读
- xquery - 将结果连接到另一个变量 XQuery
- configuration - 警报分支上的 SOA bpel 选择活动将超时外部化
- javascript - 是否可以将 Angular HTML 表中的数据传递给组件,然后循环遍历其行进行处理?
- docker - 将证书添加到 Dockerfile (rhel7)
- scala - 在 Play for Scala 中使用 OK() 下载二进制文件时指定 MIME 类型
- java - java - 迭代集合并为每次迭代调用方法或将集合传递给方法并迭代它
- spring-cloud-stream - Kafka - 延迟绑定,直到复杂的服务初始化完成
- javascript - AJAX 调用 PHP 页面,没有 error_log();
- python - 无法在 Python 中安装 64 位 Pygame
- python - Python FTP 无法完成命令