2

Following is a sample code compiled using GNU compiler (g++ command) on an Ubuntu OS 16.04:

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

} 

When compiled; I get this message:

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";

Upon execution:

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.

Entire gdb debug can be found here.

Question : Why won't the file contents be read when the file is legit and the compiler throws no error?

Ts.mp4 permissions

vedant gala
  • 428
  • 3
  • 18
  • You have lot of different and unrelated questions. Please only one question per question. – Some programmer dude Nov 08 '17 at 09:05
  • oh but I have only one question why am I unable to open the file – vedant gala Nov 08 '17 at 09:09
  • Then it's a lot of unrelated information that we don't really need. And to answer your question: The file you look for is part of the system C library, and you should not have those sources unless you're a developer of the library itself. The "missing" files are irrelevant for any normal debugging. Go `up` in the callstack until you come to your code instead. – Some programmer dude Nov 08 '17 at 09:13
  • Oh and listen to your compiler and its warnings. Don't just dismiss them. – Some programmer dude Nov 08 '17 at 09:14
  • To execute the file, such as mp4 either you need to use a system call within your code or have a library that can execute (read, parse...) it. However, you should be able to `read` with `fread` I presume. – macroland Nov 08 '17 at 09:19
  • @Someprogrammerdude Hi, I just edited my question and removed certain parts that might have misled you into assuming that there was more than one question asked. I request you to have a look again? And yes, will listen to compiler and its warnings hereafter. Thanks for that. – vedant gala Nov 08 '17 at 09:20
  • @macroland actually I would like to read into a buffer and then operate on those Mpeg packets further - I narrowed down the problem with my bigger code into an MCVE ( I hope this falls under the cateogory of an MCVE) – vedant gala Nov 08 '17 at 09:21
  • 1
    Use std::string, std::vector, std::array, fstream not char pointer and open. –  Nov 08 '17 at 09:22
  • 1
    `if (filedes==0) std::cout<<"\n File cannot be opened";` is wrong. `open()` returns `-1` on failure, not zero. `int ret = read(...); is also incorrect. `read()` returns `ssize_t` and not `int` - although that's probably not causing any problems with your code, it can cause problems in certain circumstances. You can use `perror( "read()" )` to translate the `errno` value into a string that's output to `stderr`. – Andrew Henle Nov 08 '17 at 12:28
  • 1
    @manni66 *Use std::string, std::vector, std::array, fstream not char pointer and open.* Under all those layers of code, what do you think `std::vector`, `std::array`, and `fstream` use to actually read data into a process's address space? Perhaps the OP is trying to learn how things actually work and *wants* to use `read()`. – Andrew Henle Nov 08 '17 at 12:43
  • @AndrewHenle perhaps, but most likely the OP is learning C with classes. –  Nov 08 '17 at 12:46

1 Answers1

5

Assuming you're running Linux, an errno value of 14 is EFAULT, or "bad address".

Given the code

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

pBuffer is not initialized or otherwise set, so the value in pBuffer is indeterminate and it certainly doesn't point to a valid address.

You need to actually provide a buffer where read() can place the data read:

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

would work, as long as NumBytesToRead does not exceed the number of bytes in buffer. Note also that ret is now the proper ssize_t instead of int.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56