C fopen по сравнению с открытым

из msdn

double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));

Показывает 1 234 567 890

201
задан tshepang 27 February 2014 в 03:52
поделиться

5 ответов

First, there is no particularly good reason to use fdopen if fopen is an option and open is the other possible choice. You shouldn't have used open to open the file in the first place if you want a FILE *. So including fdopen in that list is incorrect and confusing because it isn't very much like the others. I will now proceed to ignore it because the important distinction here is between a C standard FILE * and an OS-specific file descriptor.

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 (though the world appears to be converging on LF-only (except IETF text-based networking protocols like SMTP and HTTP and such)).
  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 (aka fsetpos or fseek the second of which is slightly trickier to use in a standards compliant way), the usefulness of buffering quickly goes down.

Of course, my bias is that I tend to work with sockets a whole lot, and there the fact that you really want to be doing non-blocking IO (which FILE * totally fails to support in any reasonable way) with no buffering at all and often have complex parsing requirements really color my perceptions.

229
ответ дан 23 November 2019 в 05:04
поделиться

Unless you're part of the 0.1% of applications where using open is an actual performance benefit, there really is no good reason not to use fopen. As far as fdopen is concerned, if you aren't playing with file descriptors, you don't need that call.

Stick with fopen and its family of methods (fwrite, fread, fprintf, et al) and you'll be very satisfied. Just as importantly, other programmers will be satisfied with your code.

11
ответ дан 23 November 2019 в 05:04
поделиться

open() is a low-level os call. fdopen() converts an os-level file descriptor to the higher-level FILE-abstraction of the C language. fopen() calls open() in the background and gives you a FILE-pointer directly.

There are several advantages to using FILE-objects rather raw file descriptors, which includes greater ease of usage but also other technical advantages such as built-in buffering. Especially the buffering generally results in a sizeable performance advantage.

49
ответ дан 23 November 2019 в 05:04
поделиться

Если у вас есть ФАЙЛ * , вы можете использовать такие функции, как fscanf , fprintf и fgets ] и т. д. Если у вас есть только дескриптор файла, у вас есть ограниченные (но, вероятно, более быстрые) процедуры ввода и вывода чтение , запись и т. д.

11
ответ дан 23 November 2019 в 05:04
поделиться

Использование «открывать, читать, писать» означает, что вам нужно беспокоиться о взаимодействии сигналов.

Если вызов был прерван обработчиком сигнала, функции вернут -1 и установят для errno значение EINTR.

Таким образом, правильным способом закрытия файла будет

while (retval = close(fd), retval == -1 && ernno == EINTR) ;
7
ответ дан 23 November 2019 в 05:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: