cfilebyte

read and write files open() read()


i need to make a program that read a file(arg[1]) and show it content but, with a specific number of bytes[arg2]. If no bytes are specified whole file will be printed.

void read(char *arg[]){
        int fh;
        char buffer[100];
        int rd;

        fh = open(arg[1],O_RDONLY);
        printf ("Printing %s\n",arg[1]);
        while (rd = read(fh,buffer,100)) {
                buffer[rd] = '\0';
                printf("%s",buffer);
                }
        }

I need to use open(),read() at least. Can someone help me?


Solution

  • There are few observation about the program which you wrote,

    1. void read(char *arg[]) here from main() function you are just passing the file name then whats the need of catching file name with array of char pointer, catching with single char pointer is sufficient. So modify it like void read(char *arg)

    2. fh = open(arg[1], O_RDONLY); open() is system call, whether its able to open the file or not,catch the return value & try to print error message using perror(). So modify its like

    fh = open(arg[1], O_RDONLY);
    if (fh == -1) {  
        perror("open");
        return;
    }
    
    1. while (rd = read(fh,buffer,100)) { //some code } why rotating loop ? you can read entire data of file at a time, for that use stat() system call and find the size of file and then create dynamic array equivalent to file size and then read whole/particular data using read() system call.

    So first go through man pages of open(), read(), stat() system calls.

    Here is my solution for your requirements with explaination

    #include <stdio.h>
    #include <malloc.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    void my_read(char *arg1){
            int fh;
            //  char buffer[100];//don't use static array bcz we don't know how much data is there in file, create it dynamically
            int rd,n;
    
    
            fh = open(arg1,O_RDONLY);
            if(fh == -1)
            {
                    perror("open");
                    return;
            }
            else
            {
                    //first find the size of file .. use stat() system call
                    struct stat v;
                    stat(arg1,&v);//storing all file related information into v
                    int size = v.st_size;//st.szie is a member of stat structure and it holds size of file
                    //create dynamic array equal to file size 
                    char *p = malloc(size * sizeof(char));
    
                    printf("enter no of bytes you want to read :\n");
                    scanf("%d",&n);
    
                    if(n==0)
                    {
                            //read data from file and copy into dynamic array and print 
                            int ret = read(fh,p,size);
                            if(ret == -1)
                            {
                                    perror("read");
                                    return ;
                            }
                            else
                            {
                                    printf("data readed from file is :\n");
                                    printf("%s\n",p);
                            }
    
                    }
                    else
                    {
                            //read data from file and copy into dynamic array and print 
                            int ret = read(fh,p,n);
                            if(ret == -1)
                            {
                                    perror("read");
                                    return ;
                            }
                            else
                            {
                                    printf("data readed from file is :\n");
                                    printf("%s\n",p);
                            }
                    }
    
             if (close(fh) < 0) 
             {
                 perror("c1");
                 return;
             }
    }
    int main(int argc,char *argv[])
    {
            char f_name[100];
            printf("enter the file name :\n");
            scanf("%s",f_name);
            my_read(f_name);
    }