-1

I have created a file in /proc named "test" (it was created in kernel). The file exists. When I want to open it in user level it returns negative.

int fd;
if((fd=open("/proc/test","O_RDONLY"))<0){ 
      perror("open"); 
}

The error that I see is open: File exists. I have seen this question but it is not my case.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sara
  • 2,308
  • 11
  • 50
  • 76

1 Answers1

3

You need parentheses in there (now fixed in the question), and the second argument to open() is not a string:

#include <fcntl.h>

int fd;
if ((fd = open("/proc/test", O_RDONLY)) < 0)
    perror("open");

I'm not convinced it was a good idea to create a file of any sort in the /proc file system. In fact, I'm a bit surprised you were allowed to. If you are learning to program as root, I hope you have good backups.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • After doing like above it says: O_RDONLY undeclared. I have included – Sara Jul 24 '13 at 05:40
  • 1
    It is hard to help you accurately if you don't show the code you've got problems with accurately. But the second argument should not be a quoted string (`fopen()` takes a string for its second argument; `open()` does not). And are you running as root? And what are the permissions on `/proc/test`? Look at the link I gave you; it is `#include ` for `open()`. – Jonathan Leffler Jul 24 '13 at 05:41
  • 1
    @sweet I think you need to read some tutorials. These are very basic, beginner questions that could be answered by any decent tutorial or book. – Jonathon Reinhart Jul 24 '13 at 05:42
  • I have compile error after removing quote from second argument. – Sara Jul 24 '13 at 05:43