Note for fopen and note for fopen
Organized from https://msdn.microsoft.com/zh-cn/library/t3ayayh1 (VS.80). aspx
Errno, _ doserrno, _ sys_errlist, and _ sys_nerr
These global variables include the error code used by the perror and strerror functions to print error messages. Use the more secure feature versions listed in the following table to replace their global variables.
Global Variable |
Functional Equivalents |
_ Doserrno |
_ Get_doserrno, _ set_doserrno |
_ Errno |
_ Get_errno, _ set_errno |
These variables are declared in STDLIB. H
extern int _doserrno; extern int errno; extern char *_sys_errlist[ ];extern int _sys_nerr;
Remarks
Errno is set for errors in system-level calls. Because errno retains the value set for its last call, this value may be changed by subsequent calls. Always check errno before and after possible calls.
In the error, errno is not necessarily set to the same value as the error code returned by the system call. For I/O operations, use _ doserrno to access the operating system error code of errno code. For most non-I/O operations, the value of _ doserrno is undefined.
Each error value is associated with an error message that can be printed using perror or stored in a string using strerror. Perror and strerror use the _ sys_errlist array and _ sys_nerr (number of elements in _ sys_errlist) to process error information.
The library mathematical routine sets errno by calling _ matherr. To handle mathematical errors, write your own routine according to the _ matherr reference description and name it _ matherr.
All ERRNO values defined as pre-defined constants in errno. H are UNIX compatible and listed below. Only ERANGE, EILSEQ, And EDOM are specified in the ANSI standard.
Constant |
System error message |
Value |
EPERM |
Operation not permitted |
1 |
ENOENT |
No such file or directory |
2 |
ESRCH |
No such process |
3 |
EINTR |
Interrupted function |
4 |
EIO |
I/O error |
5 |
ENXIO |
No such device or address |
6 |
E2BIG |
Argument list too long |
7 |
ENOEXEC |
Exec format error |
8 |
EBADF |
Bad file number |
9 |
ECHILD |
No spawned processes |
10 |
EAGAIN |
No more processes or not enough memory or maximum nesting level reached |
11 |
ENOMEM |
Not enough memory |
12 |
EACCES |
Permission denied |
13 |
EFAULT |
Bad address |
14 |
EBUSY |
Device or resource busy |
16 |
EEXIST |
File exists |
17 |
EXDEV |
Cross-device link |
18 |
ENODEV |
No such device |
19 |
ENOTDIR |
Not a directory |
20 |
EISDIR |
Is a directory |
21 |
EINVAL |
Invalid argument |
22 |
ENFILE |
Too program files open in system |
23 |
EMFILE |
Too program open files |
24 |
ENOTTY |
Inappropriate I/O control operation |
25 |
EFBIG |
File too large |
27 |
ENOSPC |
No space left on device |
28 |
ESPIPE |
Invalid seek |
29 |
EROFS |
Read-only file system |
30 |
EMLINK |
Too many links |
31 |
EPIPE |
Broken pipe |
32 |
EDOM |
Math argument |
33 |
ERANGE |
Result too large |
34 |
EDEADLK |
Resource deadlock wocould occur |
36 |
EDEADLOCK |
Same as EDEADLK for compatibility with older Microsoft C versions |
36 |
ENAMETOOLONG |
Filename too long |
38 |
ENOLCK |
No locks available |
39 |
ENOSYS |
Function not supported |
40 |
ENOTEMPTY |
Directory not empty |
41 |
EILSEQ |
Illegal byte sequence |
42 |
STRUNCATE |
String was truncated |
80 |
Note: fopen has a trap. Of course, it is only for beginners.
The fopen method is defined as follows:
FILE*fopen(const char * __restrict __filename, const char * __restrict __mode) __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fopen));
Fopen has two parameters: one is the file name _ filename you want to operate, and the other is the operation _ mode you want to perform. There is nothing to say about the mode. The filename parameter, if you only upload the file name you want to operate, and your file is local, you may be prompted that the file cannot be found due to Path Problems. Error = 2 (No such file or directory) There is No relevant path or this file does not exist.
In this case, there are two solutions:
One is to use w's mode locally to create a file. to operate on this file, you can ensure that the file is in your current directory, but this is not necessarily applicable, for example, I want to read a bind file of a font, and then convert the Unicode font matrix in the font file into utf8, store it in an array, and write it into the file.
The second method is relatively simple. You can try the absolute path, find the absolute path of the file, and set the parameter _ filename to OK. Note that, paths generally contain "\" characters, which are translated in C/C ++;
All escape characters and their meanings:
Escape characters |
Meaning |
ASCII value (decimal) |
\ |
Bell (BEL) |
007 |
\ B |
Return to BS to move the current position to the previous column. |
008 |
\ F |
Change page (FF), move the current position to the beginning of the next page |
012 |
\ N |
Line feed (LF), move the current position to the beginning of the next line |
010 |
\ R |
Press enter (CR) to move the current position to the beginning of the line |
013 |
\ T |
Horizontal tabulation (HT) (jump to the next TAB) |
009 |
\ V |
Vertical tabulation (VT) |
011 |
\\ |
Represents a backslash character ''\' |
092 |
\' |
Represents a single quotation mark (apostrophes) character |
039 |
\" |
Represents a double quotation mark character |
034 |
\? |
Represents a question mark |
063 |
\ 0 |
NULL) |
000 |
\ Ooo |
Any character from 1 to 3 octal digits |
Three octal nodes |
\ Xhh |
Any character in hexadecimal notation between 1 and 2 |
Two hexadecimal |
Note: Yes. slashes: "/" and backslash: "\" are not interchangeable here.Therefore, if your path is c: \ demo \ abc.txt, You need to input the parameter _ filename = c: \ demo \ abc.txt
1 #include <iostream> 2 #include <fstream> 3 4 int main () 5 { 6 std::cout << "hello world \n"; 7 8 FILE *fp; 9 char *ch = "";10 char buff[100];11 12 errno_t err = fopen_s(&fp,"c:\\Users\\zhi\\Desktop\\abc.txt","r");13 14 if(err == 0)15 {16 printf("the file open successful!");17 18 ch = fgets(buff,100,fp);19 printf("ch is :%s",ch);20 fclose(fp);21 22 }else{23 printf("err:%d",err);24 printf("the file open not OK!");25 }26 return 0;27 }
These are all problems encountered yesterday, but they have grown up.