简体   繁体   中英

read integer from file with read() c

i have a problem with file read() function. My file is like this:

4boat
5tiger
3end

Where the number is the length of the string that follows. I need to read integer and string from input file and print them out on stdoutput, using low level I/O. This is my code:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>

int main(int argc, char *argv[]){
    int *len, fd, r_l, r_s;
    char *s;
    fd=open(argv[1], O_RDONLY);
    if(fd>=0){
        do{
            r_l=read(fd, len, sizeof(int));
            r_s=read(fd, s, (*len)*sizeof(char));
            if(r_l>=0){
                write(1, len, sizeof(int));
                write(1, " ",sizeof(char));
            }
            if(r_s>=0)
                write(1, s, (*len)*sizeof(char));
        }while(r_l>=0 && r_s>=0);
    }
    return 0;
}

But it not works =/

You did not allocate space for the poitner len , you need to allocate space for it and you can simply do it by declaring it as int len; so it gets allocated in the stack and you don't need to handle it's allocation manually, so it would be something like this

int main(void) {
    int len, fd, r_l, r_s;
    char *s;
    fd = open(argv[1], O_RDONLY);
    if (fd >= 0) {
        do {
            r_l = read(fd, &len, sizeof(int));
            s   = malloc(len); /* <--- allocate space for `s' */
            r_s = 0;
            if (s != NULL)
                r_s = read(fd, s, len);
            if (r_l >= 0) {
                write(1, &len, sizeof(int));
                write(1, " ", 1);
            }
            if ((r_s >= 0) && (s != NULL))
                write(1, s, len);
            free(s);
        } while (r_l >= 0 && r_s >= 0);
        close(fd);
    }
    return 0;
}

you also didn't allocate space for s which is another problem, I did allocate space for s in the corrected code above by using malloc() .

And sizeof(char) == 1 by definition, so you don't need that.

Although, the code above will not have the errors your code has, which invoke undefined behavior, it will not do what you expect, because your data cannot be read with this algorithm.

The numbers in your file are not really integers, they are characters, so what you really need is this

int main(void) {
    char chr;
    int len, fd, r_l, r_s;
    char *s;
    fd = open(argv[1], O_RDONLY);
    if (fd >= 0) {
        do {
            r_l = read(fd, &chr, 1);
            len = chr - '0';
            s   = malloc(len); /* <--- allocate space for `s' */
            r_s = 0;
            if (s != NULL)
                r_s = read(fd, s, len);
            if (r_l >= 0) {
                printf("%d ", len);
            }
            if ((r_s >= 0) && (s != NULL))
                write(1, s, len);
            free(s);
        } while (r_l >= 0 && r_s >= 0);
        close(fd);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM