繁体   English   中英

Linux C open()失败

[英]Linux C open() Failing

我的文件open()一直失败。 该程序接受文件名的参数。 可执行文件就在我的测试文件“ file.txt”旁边。

./runProgram file.txt 
ERROR - open() failed: Invalid argument

问题是参数是一个字符串,我可以用printf()很好。 因此,我不确定为什么它是无效的参数...我正在尝试打开文件,以便获取系统的路径名限制。

#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;
}

并非open()调用失败,而是fpathconf()调用(可能是偶然的)产生了相同的错误消息。 和错误消息告诉,因为你实际使用了错误的宏参数来fpathconf()该宏fpathconf()全部开始_PC_PATH_MAX是最大路径大小的默认值 ,而不是该属性的关键。 密钥宏称为_PC_PATH_MAX

fpathconf(inputFD, _PC_PATH_MAX);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM