All pastes #358024 Raw Edit

System Call Testing

public text v1 · immutable
·published 2007-02-16 01:14 UTC
rendered paste body
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

int main()
{
        int fd;
        int n;
        fd = open("data_file.txt",O_RDONLY);
        char buff[10];
        char data[]="This";
        memset(buff,0,sizeof(buff));

        if (fd < 0)
        {
                printf("File not created creating a new file");
//              fd = open("data_file.txt",O_RDONLY|O_WRONLY|O_CREAT,0777);
                fd = open("data_file.txt",O_RDWR|O_CREAT,0777);
                if (fd < 0)
                {
                        printf("Some error in creating the file \n");
                        exit(0);
                }

        }
        printf("File already existing \n");
        n = read(fd,buff,5);
        if (n < 0)
        {
                printf("Coulnt not able to read from the file \n");
                exit(0);
        }
        printf("%s \n",buff);

        lseek(fd,0,SEEK_END);
        n = write(fd,data,5);
        if (n < 0)
        {
                printf("Coulnt not able to write on to the file \n");
                exit(0);
        }
        close(fd);


        return 0;
}

Output
-------
File already existing

Coulnt not able to write on to the file