Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Whenever I try to create an ACTUAL file in one of my directories (from a program

ID: 3626583 • Letter: W

Question

Whenever I try to create an ACTUAL file in one of my directories (from a program) using open, once I leave the program and I check for that file, it is not there. However, when I use fopen, the file is created and I can see it in my directory.

Why is this? I should be using the correct flags ...

temp = open("tester", O_RDWR|O_CREAT|O_APPEND, S_IRWXU);
(then i don't see tester in the directory)

fp = fopen("tester", "w");
(then it shows up).

However, for this program its pretty important that I use a file descriptor instead of a FILE* so i'd really like to know how to skip this minor obstacle.

Please help me out and explain this to me!
I will rate with lifesaver if you are detailed =) .

Explanation / Answer

There are four main reasons to use fopen instead of open. 1. fopen provides you with buffering IO that may turn out to be a lot faster than what you're doing with open. 2. fopen does line ending translation if the file is not opened in binary mode, which can be very helpful if your program is ever ported to a non-Unix environment. 3. A FILE * gives you the ability to use fscanf and other stdio functions. 4. Your code may someday need to be ported to some other platform that only supports ANSI C and does not support the open function. In my opinion the line ending translation more often gets in your way than helps you, and the parsing of fscanf is so weak that you inevitably end up tossing it out in favor of something more useful. And most platforms that support C have an open function. That leaves the buffering question. In places where you are mainly reading or writing a file sequentially, the buffering support is really helpful and a big speed improvement. But it can lead to some interesting problems in which data does not end up in the file when you expect it to be there. You have to remember to fclose or fflush at the appropriate times. If you're doing seeks, the usefulness of buffering quickly goes down.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote