I'm trying to build a C portable code (for Windows, MacOS and Linux) that creates an output .txt
file to receive the results of a numerical simulation.
Summarizing, the code takes the name of the file and the extension and checks if the file already exists in the directory. If so, it creates another file with the same name, but with a number between parenthesis (#) in the end to distinguish the old from the new one.
The problem is: it is working properly on the mac environment, however when I compile and run it on windows, the file is not created in the end of execution. I could not find what I'm doing wrong.
Also, I'm using Intel C/C++ Classic compiler. If I use another compiler, for example, the Intel® oneAPI DPC++/C++ Compiler for windows, it complains about the usage of sizeof(src)
when I call the strncat(...)
function.
So far, this is the version of the code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool file_exists(char *filename);
FILE *create_output_file(char *name, char *ext, bool (*file_exists)(char *));
#define funcname "test"
int main(void) {
// Create output files to store results
char *output_filename = malloc(200 * sizeof(*output_filename));
sprintf(output_filename, "%s_out", funcname);
printf("output_filename = %s\n", output_filename);
char *ext = ".txt";
FILE *output_file = create_output_file(output_filename, ext, file_exists);
}
bool file_exists(char *filename) {
// Try to open file with same name as filename
FILE *testfile = fopen(filename, "r");
// Bool variable to check the existence of file
bool exists = false;
// Check the existence of a file called filename
if (testfile != NULL) {
// Returns true if the file exists
exists = true;
}
// Close the file
fclose(testfile);
// Returns the existence of a file (1) = does exist, (0) = does not exist
return exists;
}
FILE *create_output_file(char *name, char *ext, bool (*file_exists)(char *)) {
// Form the full filename
name = strncat(name, ext, sizeof(ext));
printf("fullfilename = %s\n", name);
// Check if a file with the filename exists in the directory
if (file_exists(name)) {
// If exists, assign the same name with "(number)" to differentiate the new version
int j = 1;
char *numb = malloc(10 * sizeof(*numb));
sprintf(numb, "(%i)", j);
// Remove the extension from the name string
name[strlen(name) - strlen(ext)] = '\0';
// Add (number) to the name and then add the file extension again
name = strncat(name, numb, sizeof(numb));
name = strncat(name, ext, sizeof(ext));
// Check if the name with numbers exists until it doesn't
int limit = 1e1;
while (file_exists(name)) {
j++;
sprintf(numb, "(%i)", j);
if (j == limit) {
name[strlen(name) - strlen(numb) + 1 - strlen(ext)] = '\0';
limit = limit*10;
} else {
name[strlen(name) - strlen(numb) - strlen(ext)] = '\0';
}
name = strncat(name, numb, sizeof(numb));
name = strncat(name, ext, sizeof(ext));
}
// Free allocated memory
free(numb);
}
// After assign the proper name, create the output file
FILE *output_file = fopen(name, "w");
// Returns the file
return output_file;
}
What am I missing here?