15740 Imp Programs

download 15740 Imp Programs

of 13

Transcript of 15740 Imp Programs

  • 7/30/2019 15740 Imp Programs

    1/13

    (1)//c program to copy content of one file to

    anotherwithout using buffer.

    #include

    #include

    #include

    int main()

    {

    char c;

    int in,out;

    in=open("a.txt",O_RDONLY);

    out=open("b.txt",O_WRONLY|O_CREAT,S_IRUSR|

    S_IWUSR);

    while(read(in,&c,1)==1)

    write(out,&c,1);

    }

    (2)//c program to print fibonicci series till first 15 term

    using recursion.

    #include

    #include

    #include

  • 7/30/2019 15740 Imp Programs

    2/13

    void main()

    {

    int n=15,i,a=0,rec;printf("fibo series is \n");

    printf("%d\n",a);

    for ( i=0; i< n-1; i++ )

    {

    rec=fib(i);

    printf("%d\n",rec);

    }

    }

    int fib(int x)

    {

    if(x

  • 7/30/2019 15740 Imp Programs

    3/13

    {

    FILE *p;

    char sen[20];printf("enter the sentence");

    fgets(sen,20,stdin);

    p=fopen("myfile.txt","a");

    fputs(sen,p);

    fclose(p);

    }

    (4)//c program to read name and age from a file and then

    display on the monitor.

    #include

    int main()

    {

    FILE *p;

    int age;char name[20];

    p=fopen("st.txt","r+");

    printf("enter name and age");

  • 7/30/2019 15740 Imp Programs

    4/13

    fscanf(p,"%s",name);

    fscanf(p,"%d",&age);

    fclose(p);p=fopen("st1.txt","w");

    fprintf(stdout,"%s",name);

    fprintf(stdout,"%d",age);

    fclose(p);

    }

    (5)//c program to copy specific content from specified

    position from one file to another

    #include

    int main()

    {

    FILE *p,*q;

    char buffer[15];

    p=fopen("anu.txt","r");fseek(p,15,SEEK_SET);

    fgets(buffer,15,"anu.txt");

    q=fopen("pam.txt","w");

  • 7/30/2019 15740 Imp Programs

    5/13

    fputs(buffer,q);

    fclose(p);

    fclose(q);}

    (6)//c program to use lseek

    #include

    #include

    #include

    void main()

    {

    int fd,x;

    fd=open("app.txt",O_RDWR);

    x=lseek(fd,30,SEEK_CUR);

    printf("%d\n",x);

    close(fd);

    }

    (7)//c program to check user name.

    #include

    #include

  • 7/30/2019 15740 Imp Programs

    6/13

    #include

    void main()

    {char name[10];

    name=system("whoami");

    if(name=="sangeeta")

    {

    printf("hello");

    printf("gud morning");

    }

    else

    printf("bye");

    }

    (8)//c program to print a to z in a file

    #include

    void main()

    {

    FILE *p;

    char c;

  • 7/30/2019 15740 Imp Programs

    7/13

    p=fopen("dic.txt","r+");

    if(p!=NULL)

    {for(c='a';c

  • 7/30/2019 15740 Imp Programs

    8/13

    {

    printf("directory is created");

    }else

    printf("not created");

    }

    (10)// c program to read from one file and write in other

    using buffer.

    #include

    #include

    #include

    int main()

    {

    size_t fd,fd1,n;

    char buffer[40];

    fd=open("a.txt",O_RDWR);

    fd1=open("b.txt",O_RDWR);

    n=read(fd,buffer,40);write(fd1,buffer,40);

    close(fd);

    close(fd1);

  • 7/30/2019 15740 Imp Programs

    9/13

    }

    (11)//to list all files names in a directory with i-node

    number

    #include

    #include

    int main()

    {

    DIR *p;

    struct dirent *dp;

    if((p=opendir("dolly"))==NULL)

    printf("error");

    while(dp=readdir(p))

    {

    printf("%s\n",dp->d_name);

    printf("%ld\n",dp->d_ino);

    }

    closedir(p);

    }(12)//to find out which has more no of files among two

    two directories

    #include

  • 7/30/2019 15740 Imp Programs

    10/13

    #include

    int main()

    {DIR *p,*q;

    int c=0,c1=0;

    struct dirent *dp;

    p=opendir("first");

    while((dp=readdir(p))!=NULL)

    c=c+1;

    q=opendir("second");

    while((dp=readdir(q))!=NULL)

    c1=c1+1;

    if(c>c1)

    printf("first is larger ");

    else

    printf("second is larger");

    }

    (13)//to find out which has more content among two files

    #include

    int main()

  • 7/30/2019 15740 Imp Programs

    11/13

    {

    FILE *p,*q;

    int x,y;p=fopen("first.txt","r+");

    fseek(p,0,SEEK_END);

    x=ftell(p);

    q=fopen("second.txt","r+");

    fseek(q,0,SEEK_END);

    y=ftell(q);

    if(x>y)

    printf("first.txt is larger");

    else

    printf("second.txt is larger");

    }

    (14)//to make a directory and print error no when it is

    already present#include

    #include

    #include

  • 7/30/2019 15740 Imp Programs

    12/13

    #include

    void main()

    {int x,y;

    x=mkdir("dir1",S_IRWXU);

    if(x==0)

    printf("directory created");

    else

    y=errno;

    printf("error is""%d",y);

    }

  • 7/30/2019 15740 Imp Programs

    13/13