0

I’m working on a project and I’m in doubt which one of

FILE *ptr_file;
ptr_file = fopen("input.txt", "ab+");

or

FILE *ptr_file;
ptr_file = fopen("input.txt", "wb+");

fits the requirements better.

If the file does not exist already, it is created; otherwise, its content is deleted. New records entered by the user are always appended to existing records; however, the user can also modify existing records in the file.

No further explanation is offered and I am hoping someone more experienced can explain which mode to use for opening the said file to use in further methods. I tried to include as much info as possible yet as to not cheat.

Palec
  • 12,743
  • 8
  • 69
  • 138
TheUnknown
  • 53
  • 2
  • 10
  • The first option will not allow for "users can also modify existing records in the file". See http://stackoverflow.com/a/10631901/12711 for details. – Michael Burr Feb 18 '15 at 01:44
  • Very helpful thanks. I have a question on top of that though. If I were to make an "append()" method whilst opening it with the "wb+" param and passed in the file. Would I have to store the data from the file into an array fist and then append the user data at "strlen(array)+1"? Thanks for answering so quick. I have spent about an hour looking online for answers and could not find anything useful. – TheUnknown Feb 18 '15 at 01:48
  • 1
    it's hard to say what you'd need without more details on what you're writing and how, but I suspect that all you would need to do is is to perform an `fseek(f, 0, SEEK_END)` to the end of the file before performing the write. – Michael Burr Feb 18 '15 at 01:53
  • @TheUnknown, if you have another question, don’t post it as a comment. The comment is irrelevant to the original question. Try searching and reading first, then maybe post a new question. – Palec Feb 21 '15 at 19:26
  • 1
    By the way there are no *methods* in C. Only *functions*. – Palec Feb 21 '15 at 19:29

0 Answers0