#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    char file[MAX_PATH] = {0};

    OPENFILENAME ofn = {
        .lStructSize = sizeof(ofn),
        .lpstrFilter = "Text Files\0*.txt\0All Files\0*.*\0",
        .lpstrFile = file,
        .nMaxFile = MAX_PATH,
        .lpstrTitle = "Save File As",
        .Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT
    };

    int res = GetSaveFileNameA(&ofn);

    if (!res) {
        puts("File selection cancelled or failed!");
        exit(1);
    }

    FILE *f = fopen(ofn.lpstrFile, "w");
    if (!f) {
        perror("Error opening file for writing");
        exit(1);
    }

    if (fprintf(f, "foo") < 0) {
        perror("Error writing to file");
        fclose(f);
        exit(1);
    } else {
        printf("Successfully wrote \"foo\" to '%s'.\n", ofn.lpstrFile);
    }

    fclose(f);

    return 0;
}
