fwrite don’t work, what’s wrong with my code?
void printTree (struct recordNode* tree) {
char* report1;
FILE *fp = fopen("test.txt","w");
if (tree == NULL) {
return;
}
//if(fp) {
counter2++;
printTree(tree->right);
fwrite(fp,"%d\n", tree->pop);
//putc(tree->pop, fp);
//report1 = printf("%s = %d\n");
printTree(tree->left);
//}
fclose(fp);
}
fwritedoes not do formatted output like that, you needfprintf:fwritehas the following prototype:and, since you’re not even giving it that all-important fourth parameter (the file handle) in your call, it can pretty well whatever it pleases.
A decent compiler should have warned you about this.
You also have another problem here. Each time you call this function, you create the output file anew. That’s not good for a recursive function since each recurring call will destroy the information already written.
You may want to open the file outside of the recursive function and simply use it within there.
Something like: