0

I am trying to convert some c++ code into python for my project. but I am not getting the same output from both codes.

so the c++ code is

 int fd_frbuf;
 fd_frbuf = open("/dev/fb0", O_RDWR|O_SYNC);
 printf("%d\n", fd_frbuf); 
 if (fd_frbuf < 1) {
     printf("Invalid fb0 device file\n");
 }

the output is 3.

and my python code is

 fd_frbuf = open("/dev/fb0", "r+b")
 print(fd_frbuf.read())

output is nothing, so my check for fb0 is available or not by doing "< 1" is always false.

I have tried,

  1. fd_frbuf =  os.open("/dev/fb1", os.O_RDWR|os.O_SYNC)
    print(os.read(fd_frbuf, 1)
    
  2. tried to print differently,

    print(struct.unpack('i', fd_frbuf.read(4))[0])
    
    print(np.fromfile(fd_frbuf, dtype=np.uint32))
    

how can I read this file so that I get the same output as c++?

DYZ
  • 55,249
  • 10
  • 64
  • 93
vivek patel
  • 95
  • 1
  • 15
  • 4
    Your C++ code isn't reading from the file. Why are you calling `.read()` in the Python version? – melpomene Oct 07 '18 at 18:24
  • This will help you: [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – Oliv Oct 07 '18 at 18:38
  • @melpomene Because I was just troubleshooting . Basically I want to have same value as I got with c++ code (3). Otherwise my if condition (fd_frbuf< 1) will false in python. I am new in python so .read() printing was my first approach for troubleshooting. – vivek patel Oct 07 '18 at 20:14
  • So ... `print(fd_frbuf.fileno())`? You don't need to check for errors manually; `open` will throw an exception for you. – melpomene Oct 07 '18 at 20:18

0 Answers0