I am making a c program that can save matrices and vectors in CSV files so I can then perform operations between them. In one of my functions I create a matrix with random numbers and then save it inside a CSV file.
The problem is that I don't know how to create a different file every time the function is run so each array can be stored in a different CSV file. Because of this I have to save all the matrices inside the same file which makes the rest of the process a lot harder. How could I make the function that makes a different file every time without it having completely random names.
Here is a link to the project in replit
CodePudding user response:
How could i make the function that makes a different file every time without it having completely random names.
Some possible solutions:
- use timestamp as part of filename
- use counter
For a timestamp, example code:
char filename[80];
snprintf(filename, sizeof(filename), "prefix.%d", time(NULL));
FILE *fout = fopen(filename, "wt");
For a counter, use the same code as above, but check if the file prefix.${counter}
exists (see man access
). If it does, increment the counter and try again. If it doesn't, use the filename.