0

I'm new to WIN32 programming. I followed up a tutorial series and tried to include it to my code. I got the error FILE was not declared in this scope. Seeing this in the video it seems that it is a type. But it isn't recognised here.

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] =  '\0';
    ofn.nMaxFile = 100;
    ofn.lpstrFilter = "All Files\0*.*";
    ofn.nFilterIndex = 1;

    GetSaveFileName(&ofn);

    write_file(ofn.lpstrFile);

}
3
  • 1
    You need to #include <cstdio> (or #include <stdio.h> if you are using C and not C++) Commented Aug 2, 2021 at 11:35
  • 1
    FILE (from <cstdio>) is an old C library feature. Since you tagged C++, std::fstream (from <fstream>) might be better for you Commented Aug 2, 2021 at 11:37
  • @ IWonderWhatThisAPIDoes and @Yksisarvinen That works, thanks Commented Aug 2, 2021 at 11:41

2 Answers 2

2

The FILE structure is in the cstdio header file for C++. You could also use stdio.h but that's mostly for compatibility with C code.

That means you'll need something like this in your file before you attempt to use it:

#include <cstdio>

However, that's the legacy C stuff for C++. It works but it's not really the C++ way. If you really want to learn C++ programming, you may want to steer clear of that and use streams instead. Look into the fstream header.

Sign up to request clarification or add additional context in comments.

5 Comments

That works, thanks. I do know about what include does. I do actually have a lot of knowledge in standard c++. OOP Pointers etc. but not when it comes to api's. I forgot that I had to include cstdio.
Oooh, right, streams in C++. The worst API in all of C++' Standard Library combined with the worst possible performance characteristics. Who could possibly want to miss out? Now seriously, if you want to learn Windows Programming, don't use C's or C++'s file system abstractions. Either one exhibits essentially, what POSIX finds to be sufficient. Now let's be real, POSIX is totally not sufficient. Simply use the Windows API to access files.
@IInspectable: (as an aside, streams is ISO, not POSIX) All C++ coders are aware of the limitations and verbosity of streams but they have type safety and extensibility, two things you don't get with printf. And, if you value portability, Windows calls won't cut it. Most coders should now be using fmtlib which is now part of C++20. Far less verbose than streams but still with its stated advantages.
Sure, I/O streams are part C++' Standard Library. Though, to create an fstream, you have to somehow access the file system. And that's limited essentially by what POSIX provides. Which is completely and utterly useless. The fact that C++17 finally added a way to actually name the file system object isn't much consolidation to a design guided by a broken standard. Now if you do value portability, then realistically, the Windows API is exactly what you want to look into. If only to see what an API looks like that is ABI-stable across decades.
IInspectable, it appears your definition of portability is somewhat different to mine :-)
-1

Hi fopen is a function from C I/O standard library (stdio.h).

If you would use that function, in your C++ program, you must include that library #include <cstdio>.

But in the title you wrote C++, so in this case you can use iostream or fstream like

#include <iostream>
#include <fstream>

Read more here: fopen, stdio.h, cstdio, fstream, iostream.

Good luck!

1 Comment

i think the essence of the answer is ok, but there are some issues with wording and details. fopen is not a library. OP should #include <cstdio> (the question is about c++). When they use C++ they can (not need to) use c++ i/o. And using namespace std; is considered bad practice in most situations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.