error: FILE was not declared in this scope

error: FILE was not declared in this scope

我是 WIN32 编程新手。我跟进了一个教程系列,并尝试将其包含到我的代码中。我收到错误 FILE 未在此范围内声明。在视频中看到这个似乎是一种类型。但这里无法识别。

void write_file(char *path) {
    FILE *file;
    file = fopen(path,"wb");
        int _size = GetWindowTextLength(TextBox);
    char *data = new char [_size+1];

    GetWindowText(TextBox,data,_size+1);
    fwrite(data,_size+1,file);
}
void save_file(HWND hwnd) {
    OPENFILENAME ofn;
    char file_name[100];
    ZeroMemory(&ofn,sizeof(OPENFILENAME));

    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = hwnd;
    ofn.lpstrFile = file_name;
    ofn.lpstrFile[0] =  '[=10=]';
    ofn.nMaxFile = 100;
    ofn.lpstrFilter = "All Files[=10=]*.*";
    ofn.nFilterIndex = 1;

    GetSaveFileName(&ofn);

    write_file(ofn.lpstrFile);

}

FILE 结构在 C++ 的 cstdio header 文件中。您也可以使用 stdio.h 但这主要是为了与 C 代码兼容。

这意味着在您尝试使用它之前,您的文件中需要这样的东西:

#include <cstdio>

但是,这是 C++ 的遗留 C 内容。它有效,但它不是真正的 C++ 方式。如果你真的想学习 C++ 编程,你可能想要避开它并使用流来代替。查看 fstream header.

fopen 是 C I/O 标准库 (stdio.h) 中的一个函数。

如果您要在 C++ 程序中使用该函数,则必须包含该库 #include <cstdio>

但是在标题中你写的是C++,所以在这种情况下你可以像使用iostream或fstream一样

#include <iostream>
#include <fstream>

在此处阅读更多内容:fopen, stdio.h, cstdio, fstream, iostream

祝你好运!