converting a program from using syscalls to stdio library funcs

Attempted to switch from using syscalls(program1) to the stdio library(program2)
But while I get bytes/sec and everything else in first program, second program only prints "write: Success". How can I get the same output as program1?


(program1) - Output: 4096 bytes 10.466 sec 391.373 byte/sec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "iobench.h"

int main() {
    // opens a file called `data` with the O_SYNC
    int fd = open("data", O_WRONLY | O_CREAT | O_TRUNC, 0666);

    if (fd < 0) {
        perror("open");
        exit(1);
    }

    // writes the characer '6' to the file
    size_t size = 5120000;
    const char* buf = "6";
    double start = tstamp();

    size_t n = 0;
    while (n < size) {
        ssize_t r = write(fd, buf, 1);
        if (r != 1) {
            perror("write");
            exit(1);
        }

        // with some frequency (defined in iobench.h), prints out
        // how long it takes to complete the write.
        n += r;
        if (n % PRINT_FREQUENCY == 0) {
            report(n, tstamp() - start);
        }
    }

    close(fd);
    report(n, tstamp() - start);
    fprintf(stderr, "\n");
}



(program2) Output: write: Success
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "iobench.h"
#include <stdio.h>

int main() {

    FILE *fp;
    // opens a file called `data` with the O_SYNC
    //int fd = open("data", O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, 0666);
    fp = fopen("data", "w");
    if (fp == NULL) {
        printf("hello1");
        perror("open");
        exit(1);
    }

    // writes the characer '6' to the file
    size_t size = 5120000;
    const char* buf = "6";
    double start = tstamp();

    size_t n = 0;
    while (n < size) {
        ssize_t r = fwrite(buf, 1, sizeof(buf), fp);//write(fd, buf, 1);
        if (r != 1) {
            printf("hello2");
            perror("write");
            exit(1);
        }

        // with some frequency (defined in iobench.h), prints out
        // how long it takes to complete the write.
        n += r;
        if (n % PRINT_FREQUENCY == 0) {
            report(n, tstamp() - start);
        }
    }

    //close(fd);
    fclose(fp);
    report(n, tstamp() - start);
    printf("hello3");
    fprintf(stderr, "\n");
}

Last edited on
Well your sizeof is wrong for a start.
https://linux.die.net/man/3/fwrite

ssize_t r = fwrite(buf, 1, sizeof(*buf), fp);

I passed in buf since I thought I needed the size of const char. Could you explain why dereferencing makes sense here?

Update: This does seem to work

By passing buf, am I passing 8 bytes? Meanwhile *buf is 6 so sizeof(6) is 1? Or something like this?
Last edited on
I'd rather suggest:
size_t r = fwrite(buf, sizeof(char), strlen(buf), fp);

size
Size in bytes of each element to be written.
count
Number of elements, each one with a size of size bytes.


Here strlen() gives you the number of characters (excluding the terminating NULL char) in the C string to be written. And the size of each character is sizeof(char), which is 1 on almost all platforms.

sizeof(buf) would give you the size of a char pointer, and sizeof(*buf) would give you the size of a single char. The latter "works" as long as the C string contains exactly one character, but fails otherwise!

BTW: Return value of fwrite() is size_t, not ssize_t.

_______________

Another option, which is usually preferred when writing out strings:
1
2
const char* buf = "6";
fputs(buf, fp);
Last edited on
Thanks kigar, I will try those options out!
Topic archived. No new replies allowed.