File manipulation experiment
Experimental objectives:
- Master the use of Linux file manipulation functions
Experimental environment and working conditions:
Red Head Linux5.0 or above operating system
Experimental content:
- Design a program that requires the file "pass" to be opened. If there is no such file, create a new file with permissions set to read only by the owner.
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
int fd;
if((fd=open("pass",O_CREAT))<0)
{
printf("not exist\n");
}
printf("create file pass,it is:%d\n",fd);
chmod("pass",S_IRUSR|S_IWUSR);
return 0;
}
- Design a program that requires creating a new file "hello" and using the write function to write the "Linux software design" string to the file (if you write with the fwrite function, what should you do)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main()
{
int fd,twrite;
if((fd=open("./hello",O_CREAT|O_TRUNC|O_WRONLY,0777))<0)
{
printf("open file error");
exit(1);
}
char str[]="the design of C software under Linux";
if((twrite=write(fd,str,sizeof(str)))<0)
{
printf("write file error");
exit(1);
}
close(fd);
return 0;
}
- Design a program that requires the read function to read the system file "/ etc / passwd" and display the output in the terminal.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd,rtype,wtype;
char str[30];
if((fd=open("/etc/passwd",O_RDONLY))<0)
{
printf("read file error!");
exit(1);
}
while((rtype=read(fd,str,30))>0)
{
if((wtype=write(STDOUT_FILENO,str,rtype))<0)
printf("write file error");
}
close(fd);
return 0;
}
4. Design a program that requires opening the file "pass". If there is no such file, create a new file; read the system file "/ etc / passwd" and write the contents of the file into the "pass" file.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fdsrc,fddes,nbytes,wtype;
char str[30];
if((fddes=open("pass",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
{
printf("open file pass error");
exit(1);
}
if((fdsrc=open("/etc/passwd",O_RDONLY))<0)
{
printf("open file error");
exit(1);
}
while((nbytes=read(fdsrc,str,30))>0)
{
if((wtype=write(fddes,str,30))<0)
printf("write file error");
}
close(fdsrc);
close(fddes);
}
5. Design a program that requires 10 to be output in decimal, octal, and hexadecimal respectively (here I use the sprintf function).
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<string.h>
int main()
{
int x=10;
char FX[20],PX[20],HX[20];
sprintf(FX,"%x",x);
sprintf(PX,"%o",x);
sprintf(HX,"%d",x);
write(1,PX,strlen(PX));
printf("\n");
write(1,FX,strlen(FX));
printf("\n");
write(1,HX,strlen(HX));
printf("\n");
return 0;
}
6. Design a program that requires the creation of a new directory, with the default permissions of dr–r--r–.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
system("mkdir exercise");
chmod("exercise",0444);
return 0;
}
7. Design a program that requires the command ls -l> list to create a file, then create a soft link "ls1" and a hard link "ls2", and view the two linked files and the list file.
#include<unistd.h>
#include<stdlib.h>
int main()
{
system("ls -l > list");//>输出重定向,将当前目录下的内容输入到list中
symlink("list","l1");
link("list","l2");
system("ls list -l");
system("ls l1 -l");
system("ls l2 -l");
return 0;
}