INFO: Wrong Syntax for fopen Mode Argument Returns NULL ValueLast reviewed: August 26, 1997Article ID: Q39602 |
The information in this article applies to:
SUMMARYIn the second argument to the fopen() function, if the specification of the mode character t (text) or b (binary) is before the file-access type r (read), w (write), or a (append), no compilation errors occur. However, at run-time, fopen() fails to open the file and returns NULL. The Visual C++ Books Online and the "Microsoft C/C++ Compiler Run-Time Library Reference" state that the mode character is to be appended to the character string for the type argument. If, instead, the mode character is placed before the beginning of the type argument, then fopen fails. An example follows. Please note that the string that is passed as the second parameter to fopen could be a variable string as well as a constant string. Because the variable string could be constructed at run-time, it is impossible to check for this error at compile time.
MORE INFORMATIONThe program below demonstrates this behavior. It prints "failed" and does not open a file. If the second argument to fopen is changed to "wt", then it prints "succeeded" and the file is opened.
Sample Code
/* Compile options needed: none */ #include <stdio.h> FILE *s; void main(void); void main(void) { if ((s = fopen("test.dat","tw")) == NULL) printf("fopen failed\n"); else printf("fopen succeeded\n"); } |
Additional query words: 8.00 8.00c 9.00 9.10
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |