Monday 23 September 2013

Linux | File Programs

                 

                    File Program 1

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

int main()
{
int fd;

fd = open("test",O_RDONLY|O_CREAT,0477);

if(fd<0)
{
perror("Open");
return 0;
}


char c;
int s;

s=read(fd,&c,1);

if(s)
{
printf("\nRead Character: %c",c);
}

printf("\n\n");

close(fd);

}




 File Program 2


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

int main()
{
int fd;

fd = open("test",O_WRONLY|O_CREAT,0666);

if(fd<0)
{
perror("Open");
return 0;
}


char c[] = "Welcome To Files...";
int s;

s=write(fd,c,sizeof(c));

if(s)
{
printf("\nWritten Character: %d",s);
}

printf("\n\n");

close(fd);

}


 File Program 3


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

int main()
{
int fd;

fd = open("test",O_RDONLY|O_CREAT,0666);

if(fd<0)
{
perror("Open");
return 0;
}


char c;
int s;

while(1)
{
s=read(fd,&c,1);
if(!s)
break;
printf("%c",c);
}

printf("\n\n");

close(fd);

}




 File Program 4


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

int main()
{
int fd;

fd = open("test",O_RDWR,0644);
//fd = creat("test",0644);

if(fd<0)
{
perror("File Open");
exit(1);
}

int s,len;
char txt[100];

printf("\nEnter Text to be written to File: ");
gets(txt);

len = strlen(txt);

s = write(fd,txt,len);

printf("%d characters written to file...\n\n",s);

s = lseek(fd,0,SEEK_SET);

printf("\n[%d]\n",s);
strcpy(txt,"\0") ;
s = read(fd,txt,100);
printf("%s-%d",txt,s);
close(fd);
return 0;
}


 File Program 5

#include<stdio.h>
#include<error.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
struct stu
{
char name[40];
int rno;
double cg;
};
int main()
{
int fd;
struct stu s2,s1={"VS",333,9.9};

fd = open("test",O_RDWR|O_CREAT,0644);

if(fd<0)
{
perror("File Open");
exit(1);
}

int s;

s = write(fd,"This is Test File",10);
printf("%d characters written to file...\n\n",s);

s = write(fd,&s1,sizeof(s1));
printf("%d characters written to file...\n\n",s);

lseek(fd,-1*sizeof(s1),SEEK_CUR);
s = read(fd,&s2,sizeof(s2));
printf("Read Record: %s - %d - %lf\n\n",s2.name,s2.rno,s2.cg);

close(fd);
return 0;
}




 File Program 6

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

int main()
{
int fd;

fd = open("test",O_CREAT|O_WRONLY,0644);

if(fd<0)
{
perror("File Open");
exit(1);
}

int s,len;
char txt[100];

printf("\nEnter Text to be written to File: ");
gets(txt);

len = strlen(txt);

s = write(fd,txt,len);

printf("%d characters written to file...\n\n",s);


close(fd);
return 0;
}



 File Program 7

#include<stdio.h>
#include<sys/stat.h>
#include<error.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<time.h>

int main()
{
int fd;
struct stat st;

fd = lstat("File1.c",&st);

//fd = creat("test",0644);

if(fd<0)
{
perror("File Open");
exit(1);
}

printf("\nSize: %d",st.st_size);
printf("\nInode: %d",st.st_ino);
printf("\nDevice Number: %d,%d",major(st.st_dev),minor(st.st_dev));
printf("\nUser ID: %o",st.st_uid);
printf("\nGroup ID: %o",st.st_gid);
printf("\nMode: %o",st.st_mode);

struct tm *t;
char tt[50];
t = localtime(&st.st_atime);
strftime(tt,50,"%c",t);
printf("\nModification Time: %s",tt);
char *mt = ctime(&st.st_mtime);
printf("\nAccess Time: %s",mt);
char *ct = ctime(&st.st_ctime);
printf("\nChange Time: %s",ct);

printf("\n\n");


return 0;
}

0 comments:

Post a Comment