Home > Software engineering >  how to convert from fopen to open when trying to open a .data file?
how to convert from fopen to open when trying to open a .data file?

Time:02-14

FILE *inFile = fopen("dataFlow.dat", "r"); // read only
FILE *outFile = fopen("report.dat", "w"); // write only

CodePudding user response:

If your files have binary contents, you should open them in binary mode by appending b to the mode string.

For better portability and to take advantage of buffering, using standard streams is recommended over system call such as open, which are tricky to use reliably.

FILE *inFile = fopen("dataFlow.dat", "rb"); // read only
FILE *outFile = fopen("report.dat", "wb"); // write only

For low level system programming on POSIX systems, you can use the open() system call.

Here is a small example that just attempts to copy 1KB from one file to the other:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    unsigned char buf[1024];
    int status = 0;

    int inFile = open("dataFlow.dat", O_RDONLY);
    if (inFile < 0) {
        perror("dataFlow.dat");
        return 1;
    }

    int outFile = open("report.dat", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (outFile < 0) {
        perror("report.dat");
        close(inFile);
        return 1;
    }

    // this loop is equivalent to nread = fread(buffer, 1, sizeof buffer, inFile);
    int nread = 0;
    while (nread < (int)sizeof(buffer)) {
        ssize_t nr = read(inFile, buffer   nread, sizeof buffer - nread);
        if (nr < 0) {
            if (errno == EINTR)
                continue;
            perror("reading dataFlow");
            status = 1;
            break;
        }
        if (nr == 0)
            break;
        nread  = nr;
    }

    // this loop is equivalent to nwritten = fwrite(buffer, 1, nread, outFile);
    int nwritten = 0;
    while (nwritten < nread) {
        ssize_t nw = write(outFile, buffer   nwritten, nread - nwritten);
        if (nw < 0) {
            if (errno == EINTR)
                continue;
            perror("writing report");
            status = 1;
            break;
        }
        if (nw == 0) {
            fprintf(stderr, "cannot write data\n");
            status = 1;
            break;
        }
        nwritten  = nw;
    }
    close(inFile);
    close(outFile);

    printf("%d bytes read, %d bytes written\n", nread, nwritten);
    return status;
}

CodePudding user response:

Tag: linux

FILE *inFile = fopen("dataFlow.dat", "r"); // read only
FILE *outFile = fopen("report.dat", "w"); // write only
int inFile = open("dataFlow.dat", O_RDONLY); // read only
int outFile = open("report.dat", O_WRONLY | O_CREAT, 0666); // write only

Open is a weird function; if the call cannot create a file it takes two arguments, but if it can create a file it takes three arguments. The third argument is 0666 until proven otherwise; umask will be applied.

The return can't be passed to stdio functions except fdopen but can be used directly using read(), write(), lseek(), and close().

The return of open is a integer file handle or -1 for error.

(Posted answer while chqrle's answer was very wrong. He's proceeding to fix.)

  • Related