montimaj

AS4

Aug 14th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.52 KB | None | 0 0
  1. #include<sys/file.h>
  2. #include<sys/types.h>
  3. #include<sys/stat.h>
  4. #include<unistd.h>
  5. #include<stdio.h>
  6. int main()
  7. {
  8.   char t[100],f[30];
  9.   int fd;
  10.   printf("Enter file name: ");
  11.   scanf("%s",f);
  12.   fd=open(f,O_RDWR,S_IRWXU);
  13.   if(fd==-1)
  14.   {
  15.     fd=creat(f,S_IRWXU);
  16.     if(fd==-1)
  17.       return 1;
  18.     printf("Empty file created\n");
  19.   }
  20.   else
  21.   {
  22.     int len=read(fd,t,100),i;
  23.     printf("\nFile contents:\n");
  24.     for(i=0;i<len;++i)
  25.       putchar(t[i]);
  26.     printf("\n");
  27.   }
  28.   close(fd);
  29.   return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment