C & LINUX

C / LINUX read()

Ocean_ 2022. 5. 1. 16:17

함수 기능

오픈된 파일에서 데이터를 읽을 때 사용하는 시스템호출

함수 원형

#include <unistd.h>
Ssize_t read(int filedes, void *buf, size_t nbytes)

리턴 값 : 읽은 바이트 수, 파일의 끝에 도달한 경우 0, 에러시 -1

함수 파라메터

filedes

파일 디스크립터

 

buf

지정된 메모리

 

nbytes

nbytes 만큼 읽어라

 

fd로 부터 buf로 nbytes만큼 읽어라

함수 예제

read1

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include"ssu_employee.h"

int main(int argc, char*argv[]){
    struct ssu_employee record;
    int fd;
    int record_num;

    if(argc<2){
        fprintf(stderr, "Usage : %s file\n",argv[0]);
        exit(1);
    }
    if((fd = open(argv[1],O_RDONLY))<0){
        fprintf(stderr,"open error for %s\n",argv[1]);
        exit(1);
    }
    while(1){
        printf("Enter record number :");
        scanf("%d",&record_num);

        if(record_num<0){
            break;
        }

        if(lseek(fd,(long)record_num * sizeof(record),0)<0){
            fprintf(stderr,"lseek error\n");
            exit(1);
        }
        if(read(fd,(char *)&record,sizeof(record))>0)
            printf("Employee : %s Salary : %d\n",record.name,record.salary);
        else
            printf("Record %d not found\n",record_num);
    }

    close(fd);
    exit(0);
}

read2

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

#define BUFFER_SIZE 1024

int main(void){
    char buf[BUFFER_SIZE];
    char *fname = "ssu_test.txt";
    int count;
    int fd1, fd2;

    fd1 = open(fname,O_RDONLY,0644);
    fd2 = open(fname,O_RDONLY,0644);

    if(fd1<0 || fd2<0){
        fprintf(stderr,"open error for %s\n",fname);
        exit(1);
    }

    count = read(fd1,buf,25);
    buf[count] = 0;
    printf("fd1's first printf : %s\n",buf);
    lseek(fd1,1,SEEK_CUR);
    count=read(fd1,buf,24);
    buf[count]=0;
    printf("fd1's second printf : %s\n",buf);
    count=read(fd2,buf,25);
    buf[count]=0;
    printf("fd2's first printf : %s\n",buf);
    lseek(fd2,1,SEEK_CUR);
    count=read(fd2,buf,24);
    buf[count]=0;
    printf("fd2's second printf : %s\n",buf);

    exit(0);
    
    

}

read3 A

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

int main(void){
    char c;
    int fd;

    if((fd = open("ssu_test.txt",O_RDONLY))<0){
        fprintf(stderr,"open error for %s\n","ssu_test.txt");
        exit(1);
    }

    while(1){
        if(read(fd,&c,1)>0)
            putchar(c);
        else
            break;
    }
    exit(0);
}

read3B

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

int main(void){
    char character;
    int fd;
    int line_count = 0;

    if((fd=open("ssu_test.txt",O_RDONLY))<0){
        fprintf(stderr,"open error for %s\n","ssu_test.txt");
        exit(1);
    }

    while(1){
        if(read(fd,&character,1)>0){
            if(character == '\n')
            line_count++;
        }
        else
            break;
    }

    printf("Total line : %d\n",line_count);
    exit(0);
}

read4

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

#define TABLE_SIZE 128
#define BUFFER_SIZE 1024

int main(int argc, char *argv[]){
    static struct{
        long offset;
        int length;
    } table[TABLE_SIZE];
    
    char buf[BUFFER_SIZE];
    long offset;
    int entry;
    int i;
    int length;
    int fd;

    if(argc <2){
        fprintf(stderr,"usage: %s<file>\n",argv[0]);
        exit(1);
    }

    if((fd = open(argv[1],O_RDONLY))<0){
        fprintf(stderr,"open error for %s\n",argv[1]);
        exit(1);
    }

    entry = 0;
    offset = 0;
    while((length = read(fd,buf,BUFFER_SIZE)) > 0){
        for(i =0; i<length; i++){
            table[entry].length++;
            offset++;

            if(buf[i] == '\n')
                table[++entry].offset = offset;
        }
    }

    #ifdef DEBUG
        for(i=0; i<entry; i++)
            printf("%d : %ld, %d\n",i+1,table[i].offset,table[i].length);
    #endif

        while(1){
            printf("Enter line number : ");
            scanf("%d",&length);

            if(--length < 0)
                break;

            lseek(fd,table[length].offset,0);

            if(read(fd,buf,table[length].length)<=0)
            continue;

            buf[table[length].length] ='\0';
            printf("%s",buf);
        }
    close(fd);
    exit(0);
}

함수 결과

read1

read2

read3

실행

read4

 

리눅스시스템 프로그래밍 - 홍지만 저

교재 내에 있는 예제를 바탕으로 작성한 글 입니다.