Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues writting to existing file on FAT volumes #161

Open
deadwood2 opened this issue Sep 29, 2024 · 0 comments
Open

Issues writting to existing file on FAT volumes #161

deadwood2 opened this issue Sep 29, 2024 · 0 comments
Labels
priority:default type:bug Something isn't working

Comments

@deadwood2
Copy link
Owner

"I've replicated the FAT32 behavior (at least the one from fasttracker2-clone) with a simple code, the code is a lot similar to code used used in ft2-clone.

The program will:
First run
Verify if the new file exist, if not, create new file and exit <= file created successful
Second run
file exist, try to overwrite file <= file become 0KB (should be 1736)
Third run
file exist, wrong size trow error, try to overwrite file <=file is ok again (1736 byte)
Forth run
Same as second (0KCool and so >"

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "unicode/utypes.h"

int main() {
    const char* filename = "FT2.cfg";
    const size_t requiredSize = 1736;

    // Try to open the file in binary read mode
    FILE* file = fopen(filename, "rb");

    if (file == NULL) {
        // File does not exist, create it
        printf("The file does not exist, creating...\n");
        file = fopen(filename, "wb");
        if (file == NULL) {
            printf("Error creating the file!\n");
            return 1;
        }

        // Create a buffer of 1736 bytes and write it to the file
        char buffer[1736] = {0}; // Initialize buffer to zero
        fwrite(buffer, sizeof(char), requiredSize, file);
        fclose(file);
	// Pause and wait for user input
	printf("File created exiting, press any key to continue...\n");	
    	getchar(); // Wait for user to press any key
	return 0;
    } else {
        // File exists, check its size
        fseek(file, 0, SEEK_END);
        long fileSize = ftell(file);
        fclose(file);

        if (fileSize == requiredSize) {
            printf("The file exists, all good.\n");
        } else {
            printf("The file exists, wrong size!\n");
        }
    }

    // Pause and wait for user input
    printf("Trying to overwrite the file, press any key to continue...\n");
    getchar(); // Wait for user to press any key

    // Overwrite the file with 1736 bytes
    file = fopen(filename, "wb");
    if (file == NULL) {
        printf("Error opening the file for writing!\n");
        return 1;
    }

    char buffer[1736] = {0}; // Initialize buffer to zero
    fwrite(buffer, sizeof(char), requiredSize, file);
    fclose(file);

    printf("The file has been overwritten.\n");
    return 0;
}
@deadwood2 deadwood2 added type:bug Something isn't working priority:default labels Sep 29, 2024
@deadwood2 deadwood2 added this to ABIv0 Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority:default type:bug Something isn't working
Projects
Status: No status
Development

No branches or pull requests

1 participant