[C] How to use file IO with C

#include<unistd.h>
#include<fcntl.h>
#include<string.h>
 
const int BUFF_SIZE = 128;
 
int main(){
 
    char *error_msg = "This is Error\n";
    char buff[BUFF_SIZE];
    int file;
    ssize_t rd_size;
 
    creat("name.txt", 0644);
    file = open("name.txt", O_RDWR);
    if(file == -1){
        write(1, error_msg, strlen(error_msg));
    }else{
        while(1){
            rd_size = read(0, buff, BUFF_SIZE);
            if(strstr(buff, ":wq")){
                break;
            }
            write(file, buff, rd_size);
        }
        lseek(file, 0, SEEK_SET);
        char *temp = "-------------------------\n";
        write(1, temp, strlen(temp));
        while(0 < (rd_size = read(file, buff, BUFF_SIZE-1))){
            buff[rd_size] = '\0';
            write(1, buff, strlen(buff));
        }
        close(file);
    }
    return 0;
}

Comments

Popular posts from this blog

[OpenCV] What is gocv?

[OpenCV] Chapter01-05 Working with UI elements such as scrollbars in OpenCV window

[OpenCV] How to set up gocv