I'm having trouble writing in a file, but the problem is that weird, I dont't even know for what I should exactly ask for.
At first: I use C, I am forced to keep it concurring to the 89/90-standard an to compile with gcc. My OS is Windows 7 Home Premium, 64Bit.
What I want to do, is to open three filestreams like this:
FILE *A=fopen("A.txt", "r");
FILE *B=fopen("B.txt", "w");
FILE *D=fopen("C.txt", "w");
The first one only to read, the two others to write. During the program, I write with
fprintf(B, "%c", letter);
fprintf(D, "%c", letter);
integers, interpreted as ASCII-charachters into the files. I did this twenty times before, it always worked. Now, the "D" file stays empty. If I change the order of the streams to:
FILE *A=fopen("A.txt", "r");
FILE *D=fopen("C.txt", "w");
FILE *B=fopen("B.txt", "w");
my file "B" stays empty! So always the last one opened does not work. But why?! I can't see any difference to my other programms, which are working, and this program also works, except in the case of this third filestream. I'm compiling with -Wall and -pedantic, the Compiler is not complaining, the Programm ist not crashing, everything works but the third stream!
Has anyone any idea, or even better, experience with a problem like this? I tried about one hour without getting any clue.
Edit: Thanks for alle the comments! I'm programming with C for about two months till now, so there are many things, I'm not really sure of.
@Mhd.Tahawi: Yes, now I did, but no difference.
@Kninnug: The files get opened succesfully.
@Bit Fiddling Code Monkey: Yes, as far as I know, there is a limit somewhere. But I worked with 6 filestreams at the same time a week ago and everything was fine.
@Pandrei: Yes, the Output in the other file was fine.
@Martin James: I'm not sure what you mean. If you mean fclose or something similar: Since so many of you asked me for this, I tried it, but there was no difference.
@hmjd: 'letter' is an integer that gets its value by fgetc(A);
@squeamish ossifrage: I'm printing sign by sign into the file an get '1' for each, seems to be ok.
Edit2: Now, that's nice, I'm facing some kind of Heisenbug. I was going to check the return values, as so many of you told me to.
So I initalized a new int retval=0; and wrote:
letter=fgtec(A);
while (letter!=EOF)
{
retval=fprintf(B, "%c", letter);
printf("%d", retval); /*Want to see the return values on stdout*/
if (retval<0) /*If writing fails, the program stops*/
{
printf("Error while writing in file");
return (-1);
}
letter=fgetc(A);
}
And this one works! New file gets filled, everything is fine. Except for the fact, that I have absolutely no idea, why the old one:
letter=fgtec(A);
while (letter!=EOF)
{
fprintf(B, "%c", letter);
letter=fgetc(A);
}
that worked tenthousand times before, didn't do anything at all.
Has anyone any further ideas? However, thanks so far for your ideas!