2

以下是在 Ubuntu OS 16.04 上使用 GNU 编译器(g++ 命令)编译的示例代码:

#include<iostream>
#include<unistd.h>
#include<fcntl.h>
#include <errno.h>
int main()
{           char* pBuffer;          

            char* storedfilepath = "/home/rtpl/Desktop/ts.mp4";

            std::cout<<"\n Opening file at "<<storedfilepath<<"\n";

            int NumBytesToRead = 1000 ;
            int filedes = open(storedfilepath,O_RDONLY);

            std::cout<<"\n value of error is "<<errno<<"\n";

            std::cout<<"\n value of filedes is "<<filedes;

            if (filedes==0)
            std::cout<<"\n File cannot be opened";
            else
            {
            std::cout<<"\n File opened successfully";
            std::cout<<"\n Now reading file\n"; 

            }

            //if(
            int ret = read(filedes,pBuffer,NumBytesToRead);

            std::cout<<"\n value of error is "<<errno<<"\n";

            if(ret!= -1)
            std::cout<<"\n File read successfully";
            else
            std::cout<<"\n File contents cannot be read";   

            std::cout<<"\nEnd.\n";  

            close(filedes);
            return 0;

} 

编译时;我收到这条消息:

rtpl@rtpl-desktop:~/Desktop$ g++ -g checkts.cpp
checkts.cpp: In function ‘int main()’:
checkts.cpp:8:27: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
    char* storedfilepath = "/home/rtpl/Desktop/ts.mp4";

执行时:

rtpl@rtpl-desktop:~/Desktop$ ./a.out

 Opening file at /home/rtpl/Desktop/ts.mp4

 value of error is 0

 value of filedes is 3
 File opened successfully
 Now reading file

 value of error is 14

 File contents cannot be read
End.

可以在这里找到整个 gdb 调试。

问题:为什么当文件合法且编译器没有错误时,文件内容不会被读取?

ts.mp4 权限

4

1 回答 1

5

假设您正在运行 Linux,则errno值为 14EFAULT或“错误地址”。

给定代码

char* pBuffer;
  .
  .
  .
int ret = read(filedes,pBuffer,NumBytesToRead);

pBuffer未初始化或以其他方式设置,因此 in 的值pBuffer是不确定的,它当然不指向有效地址。

您实际上需要提供一个缓冲区read()来放置读取的数据:

char buffer[ 1024 ]
   .
   .
   .
ssize_t ret = read(filedes,buffer,NumBytesToRead);

只要NumBytesToReadbuffer超过. 另请注意,ret现在是正确的ssize_t而不是int.

于 2017-11-08T12:38:38.460 回答