Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

c - Reading from file using read() function

I have to write a program that reads numbers in separate lines each. Is it possible to read only one line of the file and then read an int from the buffer, and so on until the end of file? It is really hard to find good examples of using read and write. My example is kind of working but I'd like to read until it detects ' ' char and then convert buffer into int.

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

int fd[2];

void readFile(int fd) {
    unsigned char buffer[10];
    int bytes_read;
    int k=0;
    do {
        bytes_read = read(fd, buffer, 10); 
        k++;
            for(int i=0; i<10; i++) {
            printf("%c", buffer[i]);
        }
    }
    while (bytes_read != 0); 
}

int tab[50];

int main(int argc, char *argv[]) {
    if(argv[1] == NULL || argv[2] == NULL)   {
            printf("Function requires two arguments!
Good bye...
");
            return -1;
    }
    else {
        if (access(argv[1], F_OK) == 0) {
            fd[0] = open(argv[1], O_RDONLY);
        }
        else { 
            fd[0] = open(argv[1], O_WRONLY|O_CREAT|O_SYNC, 0700);
            const int size = 50;
                for(int i=0; i<size; i++) {
                    char buf[10];   
                    sprintf(buf, "%d
", i+1);
                    write(fd[0], buf, strlen(buf));
                }
                close(fd[0]);
            fd[0] = open(argv[1], O_RDONLY);
            if (access(argv[2], F_OK) == 0) 
                fd[1] = open(argv[2], O_WRONLY);
            else 
                fd[1] = open(argv[2], O_WRONLY|O_CREAT, 0700);
        }
    }

    if (access(argv[2], F_OK) == 0) 
        fd[1] = open(argv[2], O_WRONLY); 
    else 
        fd[1] = open(argv[2], O_WRONLY|O_CREAT, 0700);


    readFile(fd[0]);
    close(fd[0]);
    close(fd[1]);
}

Revised code:

void readFile(int fd) {
    char buffer[10];
    int bytes_read;
    int k = 0;
    do {
        char t = 0;
        bytes_read = read(fd, &t, 1); 
        buffer[k++] = t;    
        printf("%c", t);
        if(t == '
' && t == '') {
            printf("%d", atoi(buffer));
            for(int i=0; i<10; i++) 
                buffer[i]='';
            k = 0;
        }
    }
    while (bytes_read != 0); 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Read Byte by Byte and check that each byte against ' ' if it is not, then store it into buffer
if it is ' ' add '' to buffer and then use atoi()

You can read a single byte like this

char c;
read(fd,&c,1);

See read()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...