我想把它分解成多个函数我可以使用什么数据类型?

I want to break this into multiple functions what data type can i use?

还有比这更多的代码,但我正在从多个编辑控件和一个外部文件中检索数据以计算货币。我想把它分解成多个更小的函数,但我不知道它将使用什么数据类型来 运行 成功。

HANDLE hBuck = CreateFile(FileName, GENERIC_ALL, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD bytes_read;
DWORD leni = GetFileSize(hBuck, NULL);
LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, leni +1);
ReadFile(hBuck, buffer, leni, &bytes_read, NULL);
double xt = atof(buffer);
CloseHandle(hBuck);
        
DWORD len = GetWindowTextLength(hPen) +1;
buffer = (LPSTR)GlobalAlloc(GPTR, len +1);
GetWindowText(hPen, buffer, len);
double num1 = atof(buffer);

len = GetWindowTextLength(hNick) +1;
buffer = (LPSTR)GlobalAlloc(GPTR, len +1);
GetWindowText(hNick, buffer, len);
double num2 = atof(buffer);

len = GetWindowTextLength(hDime) +1;
buffer = (LPSTR)GlobalAlloc(GPTR, len +1);
GetWindowText(hDime, buffer, len);
double num3 = atof(buffer);
                    
len = GetWindowTextLength(hQuart) +1;
buffer = (LPSTR)GlobalAlloc(GPTR, len +1);
GetWindowText(hQuart, buffer, len);
double num4 = atof(buffer);

尝试这样的事情:

bool ReadDoubleFromFile(LPCSTR FileName, double *value)
{
    if (!value) return false;
    *value = 0.0;

    HANDLE hBuck = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hBuck == INVALID_HANDLE_VALUE) return false;

    DWORD len = GetFileSize(hBuck, NULL);
    if (len == INVALID_FILE_SIZE) {
        CloseHandle(hBuck);
        return false;
    }

    LPSTR buffer = (LPSTR) LocalAlloc(LPTR, len + 1);
    if (!buffer) {
        CloseHandle(hBuck);
        return false;
    }

    DWORD bytes_read;
    if (!ReadFile(hBuck, buffer, len, &bytes_read, NULL)) {
        LocalFree((HLOCAL)buffer);
        CloseHandle(hBuck);
        return false;
    }

    CloseHandle(hBuck);

    *value = atof(buffer);
    LocalFree((HLOCAL)buffer);

    return true;
}

bool ReadDoubleFromEdit(HWND hEdit, double *value)
{
    if (!value) return false;
    *value = 0.0;

    DWORD len = GetWindowTextLengthA(hEdit);
    if (len > 0) {
        ++len;

        LPSTR buffer = (LPSTR) LocalAlloc(LPTR, len);
        if (!buffer) return false;

        if (!GetWindowTextA(hEdit, buffer, len)) {
            LocalFree((HLOCAL)buffer);
            return false;
        }

        *value = atof(buffer);
        LocalFree((HLOCAL)buffer);
    }

    return true;
}

...

double xt, num1, num2, num3, num4;

if (ReadDoubleFromFile(FileName, &xt) &&
    ReadDoubleFromEdit(hPen, &num1) &&
    ReadDoubleFromEdit(hNick, &num2) &&
    ReadDoubleFromEdit(hDime, &num3) &&
    ReadDoubleFromEdit(hQuart, &num4))
{
    // use values as needed...
}
else
{
    // error
}

另一方面,如果您使用的是 C++ 而不是 C(您没有说明您使用的是哪种语言),这会变得更简单一些:

double ReadDoubleFromFile(const std::string &FileName)
{
    std::ifstream inFile;
    inFile.exceptions(std::ifstream::failbit);
    inFile.open(FileName.c_str());
    double value;
    inFile >> value;
    return value;
}

double ReadDoubleFromEdit(HWND hEdit)
{
    double value = 0.0;
    DWORD len = GetWindowTextLength(hEdit);
    if (len > 0) {
        std::basic_string<TCHAR> buffer(len, TEXT('[=11=]'));
        if (!GetWindowText(hEdit, buffer.data(), len + 1))
            throw std::runtime_error("GetWindowText failed");
        value = std::stod(buffer);
    }
    return value;
}

...

try
{
    double xt   = ReadDoubleFromFile(FileName);
    double num1 = ReadDoubleFromEdit(hPen);
    double num2 = ReadDoubleFromEdit(hNick);
    double num3 = ReadDoubleFromEdit(hDime);
    double num4 = ReadDoubleFromEdit(hQuart);
    // use values as needed...
}
catch (const std::exception &ex)
{
    // handle error as needed...
}