You know... perror is more than a french guy's name.
Printable View
You know... perror is more than a french guy's name.
Is there a way that my print function could be written in a non recursive way, all we've been thought is recursion recursion, but now im thinking, yea what i have works, but suppose the person made a mistake in inserting stuff to the binary tree, and then went back and added, when the function to print to file is called it will write at the end since its on append mode, and if i set it on "w", since its a recursive fucntion only the last record will be written, since everytime its called it overwrites the data.
Any suggestions? or am i clear enough? let me know
Simply change:
dataOutput = fopen("fileOutput.txt","a+");
to:
dataOutput = fopen("fileOutput.txt","w");
notice the "w"
yea elsewhere, but that is the issue, remember the fucntion is recursive hence if i had "w" from teh start only the last entry would be written cause the file is overwritten. What i was asking was ok, i have the file written once, which is cool, but ok user goes says, hey i forgot to add another entry, user goes adds it, then choses write to file ok again, but what happens since its on a+ its added to the end, adn the file has duplicates now, and if "w" is used only the last entry is added. Umm makes sense?
I don't fully understand the overall structure of the program, but if you add something to a binary tree, you will PROBABLY want to write the entire tree to the file next time round - or does your code somehow know that it's written most of it to the file, and only need to write a few last nodes? If it knows this, it could simply say open the file with "w" when it's the first time you write the tree, then use "a+" when you want to only write the last few nodes.
You can certainly write a non-recursive version of a binary tree printer, but it gets a bit messy trying to remember which nodes you've been to and where to go next unless you implement your own stack mechanism to track that (when you use recursion, it does that for you).
--
Mats
oh, I understand now! the problem is that you don't understand the concept,
here:
First you need to run the program in PURE memory, don't worry about files, If the user decides to save his/her data then write it to the file, this is ok the first time around, and like you said what if the user wants to add something or remove something from the file he/she saved, then simply LOAD the file, SCAN ALL DATA INTO MEMORY once more and now the user will be able to edit or do anything they want with the information and then simply write the data from memory to file again ( overwrite the file , hence "w" i said before ) and boom! there you have it done!
regards
:)
Yea guys no worries, got it solved, thanks to elsewhere
Forgot to answer this one Mats, well just to clear this out. I had a recursive function u know like if printing however writing to a file. It was able to write to the text, but if i cam back to the problem and added a few nodes, they were being written yes but if the previous were still on memory they too were written again, hence having duplicates on text file and not sorted. WHat i did was open file with "w" to clear out the previous data, then call my function again to write over the file. It came out pretty good id say. Thanks for the response though.
No problem. Admittedly I use puts() more than perror() simply out of mostly knowing the nature of the problem. But the poor end user is ultimately the casualty of not using perror() in IO errors since usually technical support staff can be more useful when called with something other than "Excuse me, I was running ______ version x.xx and I keep getting 'Invalid file operation.' when I click the submit button. What is wrong?"