fopen返回null - Perror打印无效参数

Kin*_*Jnr 1 c file-io

简单的fopen操作似乎不起作用.perror返回 - 参数无效.可能有什么不对.

我在R:中有一个名为abc.dat的ascii文件.

int main()
{
    FILE *f = fopen("R:\abc.dat","r");
    if(!f)
    {
        perror ("The following error occurred");

        return 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

发生以下错误:参数无效.

Ani*_*han 5

逃避你的\.它必须\\在字符串中使用时.

FILE *f = fopen("R:\\abc.dat","r");
Run Code Online (Sandbox Code Playgroud)

否则,字符串被视为fopen包括\a"alert"转义序列,它是一个无效的参数.

常见的逃逸序列及其目的是:

\a  The speaker beeping
\\  The backslash character

\b  Backspace (move the cursor back, no erase)
\f  Form feed (eject printer page; ankh character on the screen)
\n  Newline, like pressing the Enter key
\r  Carriage return (moves the cursor to the beginning of the line)
\t  Tab
\v  Vertical tab (moves the cursor down a line)
\’  The apostrophe
\”  The double-quote character
\?  The question mark
\0  The “null” byte (backslash-zero)
\xnnn   A character value in hexadecimal (base 16)
\Xnnn   A character value in hexadecimal (base 16)
Run Code Online (Sandbox Code Playgroud)