C alternative to fputs()/fgets() with open()

I'm learning about kernel modules communicating with user level programs and originally was opening a file with

FILE *pFile = fopen(...)

and writing to it with

char *str = malloc(10);
fputs(str, pFile);

However now I need to use functionality in #include <fcntl.h> to attempt some asynchronous notifications and the example I have instead uses

int pFile;
pFile = open("/dev/mymodule", O_RDWR);

I think that I can no longer use fputs() or fgets() because it requires a FILE *ptr and I have an int instead now. I think I'm supposed to use write() and read() instead.

Is this how it would work?

write(pFile, str,  sizeof(str));

char line[256];
    while (read(pFile, line, 256) != NULL) {
            printf("%s", line);
        }

Answers


You can open a FILE* based on the file descriptor with fdopen():

FILE *file = fdopen(pfile, "rw");

Reference: fdopen - associate a stream with a file descriptor


Need Your Help

big-endian ip address into 4 byte binary with php

php bittorrent endianness tracker

I am looking to create a private torrent tracker but I have came across a problem. I found in the protocol specifications that the compact response that the most clients use now is presented as bin...

laTex cv currvita

templates latex resume

I found this template and I would like to modify two things but all I tried does not work: