I'm working on a project for university, the basic idea is that I want to check if a file is empty.
void main() {
FILE* file = fopen("stocksData.csv", "w+");
fseek(file, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(file);
if (len == 0) { //check if the file is empty
fclose(file);
importCarStock();
}
}
If it is empty, I want it to call this function:
void importCarStock() {
FILE* file = fopen("stocksData.csv", "w");
fwrite(carStock, sizeof(char), sizeof(carStock), file);
fclose(file);
}
And write this array to the file:
int carStock[10] = { 5, 7, 10 };
But the only thing it writes to the file is two small blocks. Any idea what is wrong with this? Thanks in advance.
w+mode will empty it. Usermode.5, 7, 10are ASCII codes of something that might look like "two small blocks" in a text viewer.sprintf()orfprintf().