I think I have those pointers issues again. I have created a simple function that saves a char array into a specified file. This is all my code:
#include <stdio.h>
#include <string.h>
void Output(const char*, const char*, const char*);
void OutPutSomething();
// main.cpp --------------------------------------------------
int main(int argc, char** argv) {
char Message[256];
snprintf(Message, 256, "This message will be saved\n");
Output("Output.txt", Message, "w");
return 0;
}
void OutPutSomething() {
Output("Output.txt", "This text will not be saved (???)\n", "w");
}
void Output(const char *FileName, const char *Text, const char *Mode) {
FILE *OutputFIle;
OutputFIle = fopen(FileName, Mode);
if (OutputFIle != NULL) {
printf(Text, "\n");
fputs(Text, OutputFIle);
fclose(OutputFIle);
} else {
printf("Output function failed!");
}
}
So, my problem is this: when called from the main function the Output()
function woks correctly - the text is saved in the file. Yet, when I call the Output()
function from OutPutSomething()
it doesn't save the text into the file properly (it only saves a '\B0' text). I see the text that printf()
shows in the console but the text is not saved.
What could be the cause? Thanks!
MORE: I use Code::Blocks (GCC compiler) and the application is a console application. No libraries linked, no other headers added. Frustrating to see that such simple things don't work.
.cpp
file which can be independently compiled and then linked into the final binary.