Linux C open()失败
我的文件打开()一直失败。程序接受文件名的参数。可执行文件就在我的测试文件“file.txt”的旁边 问题是参数是一个字符串,我可以打印它。因此,我不确定为什么它是无效参数…我正在尝试打开一个文件,以便获得系统的路径名限制Linux C open()失败,c,linux,file,path,C,Linux,File,Path,我的文件打开()一直失败。程序接受文件名的参数。可执行文件就在我的测试文件“file.txt”的旁边 问题是参数是一个字符串,我可以打印它。因此,我不确定为什么它是无效参数…我正在尝试打开一个文件,以便获得系统的路径名限制 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> #include <limits.h> #include &
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char **argv)
{
printf("Pathname limit according to <limits.h> is: %i\n", PATH_MAX);
int inputFD;//file descriptor to open the file
//check and make sure that the user gave us the file name
if(argc != 2)
{
fprintf(stderr, "ERROR - No file name argument!\n");
return -1;
}
printf("%s\n", argv[1]);
inputFD = open(argv[1], O_RDWR);
if(inputFD == -1)
{
perror("ERROR - open() failed");
return -1;
}
int fpcSize = fpathconf(inputFD, PATH_MAX);//return value from fpathconf
if(fpcSize == -1)
{
perror("ERROR - open() failed");
return -1;
}
printf("Path Size Limit: %i\n", fpcSize);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
printf(“根据is:%i\n”,路径\u MAX的路径名限制);
int inputFD;//打开文件的文件描述符
//检查并确保用户给了我们文件名
如果(argc!=2)
{
fprintf(stderr,“错误-无文件名参数!\n”);
返回-1;
}
printf(“%s\n”,argv[1]);
inputFD=打开(argv[1],O_RDWR);
如果(inputFD==-1)
{
perror(“错误-打开()失败”);
返回-1;
}
int fpcSize=fpathconf(inputFD,PATH_MAX);//从fpathconf返回值
如果(fpcSize==-1)
{
perror(“错误-打开()失败”);
返回-1;
}
printf(“路径大小限制:%i\n”,fpcSize);
返回0;
}
失败的不是您的open()
调用,而是您的fpathconf()
调用(可能是偶然)产生了相同的错误消息。错误消息说明了这一点,因为您在fpathconf()
的参数中实际使用了错误的宏:fpathconf()
的宏都以\u PC\u
开头,PATH\u MAX
是最大路径大小的默认值,而不是该属性的键。键宏被称为\u PC\u PATH\u MAX
:
fpathconf(inputFD, _PC_PATH_MAX);
失败的不是您的open()
调用,而是您的fpathconf()
调用(可能是偶然)产生了相同的错误消息。错误消息说明了这一点,因为您在fpathconf()
的参数中实际使用了错误的宏:fpathconf()
的宏都以\u PC\u
开头,PATH\u MAX
是最大路径大小的默认值,而不是该属性的键。键宏被称为\u PC\u PATH\u MAX
:
fpathconf(inputFD, _PC_PATH_MAX);
您确定您的
open()
,而不是fpathconf()
失败了吗?不知何故,您对这两个都使用了相同的错误消息。您确定您的open()
,而不是fpathconf()
失败了吗?不知何故,你对这两个都使用了相同的错误消息。我知道我遗漏了一些东西。。。非常感谢你抓住了那个!我知道我错过了什么。。。非常感谢你抓住了那个!