简单的fopen操作似乎不起作用。perror返回-无效参数。有什么可能是错的。
我在R:中有一个名为abc.dat的ascii文件。
int main()
{
FILE *f = fopen("R:\abc.dat","r");
if(!f)
{
perror ("The following error occurred");
return 1;
}
}输出:
发生以下错误:无效参数。
发布于 2012-06-25 00:07:51
逃离你的\。在字符串中使用时,它必须是\\。
FILE *f = fopen("R:\\abc.dat","r");否则,fopen会看到该字符串包含\a“警报”转义序列,这是一个无效的参数。
常见的转义序列及其目的是:
\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)发布于 2012-06-25 00:08:03
您需要在文件名参数中转义反斜杠:
FILE *f = fopen("R:\\abc.dat","r");这是因为字面意思是- UNnescaped - \a是一个控制字符,通常意味着bell,即声音/显示系统警报;这是一个文件名中的无效字符。
请参阅贝尔字符
在C编程语言(1972年创建)中,钟形字符可以用\a放在字符串或字符常量中。('a‘表示“警报”或“可听”,之所以选择它是因为\b已经用于后置字符。)
发布于 2012-06-25 00:08:15
您需要转义文件名中的反斜杠:
FILE *f = fopen("R:\\abc.dat", "r");https://stackoverflow.com/questions/11182178
复制相似问题