C ++ file reading and writing program selection

Strictly speaking, there are three kinds of styles.

  1. UNIX underlying library to read and write
  2. c language standard library stdio
  3. iostream stream

General engineering, the underlying level of literacy library package is too low, abnormal scene needs its own caching and handling many generic. Is not suitable.

Network programming, the cache will lead to many negative effects, consider using low-level library to read and write.

1. Comparative formatted output

Configurability 1.1 formatted output

iostream manipulator uses to format, format information coded into the code, it is difficult to realize configurable - software engineering is absolutely flawed.

stdio% d% s used and the like have been developed into a DSL, and common in many languages.  http://en.wikipedia.org/wiki/Printf_format_string#Programming_languages_with_printf

1.2 context-free

iostream, the state is achieved by changing the formatted output stream, and the state of the flow is global. require times to prevent the influence of the code behind.

stdio is context-free.

1.3 Buffer Overflow

stdio problem is that security is not a type, but also a security risk buffer overflow. correct and safe practices are as follows

#include <stdio.h>

int main ()
{
    const int max = 80;
    char name[max];

    char fmt[10];
    sprintf(fmt, "%%%ds", max - 1);
    scanf(fmt, name);
    printf("%s\n", name);
}

1.4 type safety and compatibility with 64-bit

stdio standard library, you need to clearly know the type of data to be output, and select the appropriate% d,% ld.

Format string parameter type mismatch will result difficult to find the bug.

Considering the supports 32 and 64 in proper printing machine, more headaches select% d.

Author "The Linux Programming Interface" proposal (section 3.6.2) unified converted to type long before then "% ld" to print; for some types still require special handling, such as the type of off_t may be long long.

http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#64-bit_Portability

printf not like that int using printf to print directly print custom objects.

1.5 Performance

  1. Even if only one function interpreter, but also the entire load. As in the example above, we have to load the entire package, including floating point and string that explain part of the process, you can not reduce the length of the program. 2. Although the printf family of functions has been optimized very well, however, it is explained during operation, if they can analyze the format string in the variable during compilation, call the respective function depending on the type of treatment, will run much faster, and during type checking of C ++ compiler will help us to find errors.

Online ACM / ICPC website topic sentence, if a simple subject timeout error occurs, then the input and output of which iostream into stdio, sometimes you can cross the border.
  3. For C ++ is, printf can not be extended is its biggest drawback. We can extend it by overloads, because of overloaded functions have different types of parameters, and the function printf family type information hidden in variable parameter table and the format string.

2. Thread Safety

stdio functions are thread-safe,

And the C language also provides flockfile (3) / funlockfile (3) function like FILE * to explicitly control the locking and unlocking.

cout << a << b; twice function call corresponds cout.operator << (a) .operator << (b).

Two call center may be interrupted by a context switch, resulting in a noncontiguous output, insert a character other threads to print.

3. Memory leak

 

Reproduced in: https: //www.cnblogs.com/misspy/p/4042118.html

Guess you like

Origin blog.csdn.net/weixin_34100227/article/details/93789753