文件读写编程题目
job3/myecho.c
myecho.c的功能与系统echo程序相同
接受命令行参数,并将参数打印出来,例子如下:
$ ./myecho x
x
$ ./myecho a b c
a b c
【解】
mkdir job3 //新建文件夹
cd job3 //打开文件夹
touch myecho.c //新建文件
vim myecho.c //编辑文件
#include<stdlib.h>
#include<stdio.h>
int main(int argc,char *argv[])
{
int i;
for(i=1;i<argc;i++)
{
printf("%s ",argv[i]);
}
printf("\n");
return 0;
}
:wq 保存退出
cc myecho.c //编译
job3/mycat.c
mycat.c的功能与系统cat程序相同
mycat将指定的文件内容输出到屏幕,例子如下:
要求使用系统调用open/read/write/close实现
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
$ ./mycat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
【解】
touch mycat.c //新建文件
vim mycat.c //编辑文件
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
FILE *fp;
int fsize;
char *buffer;
if(argc!=2)
{
printf("parameter error...\n");
exit(0);
}
else
{
fp = fopen(argv[1],"r");
if(!fp)
{
printf("can't open the file!\n");
exit(0);
}
fseek(fp,0,SEEK_END);
fsize = ftell(fp);
rewind(fp);
buffer=(char*)malloc((1+fsize)*sizeof(char));
if(!buffer)
{
printf("malloc error...\n");
exit(0);
}
fread(buffer,1,fsize,fp);
printf("%s\n",buffer);
fclose(fp);
free(buffer);
return 0;
}
}
:wq 保存退出
cc mycat.c //编译
job3/mycp.c
mycp.c的功能与系统cp程序相同
将源文件复制到目标文件,例子如下:
要求使用系统调用open/read/write/close实现
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
$ ./mycp /etc/passwd passwd.bak
$ cat passwd.bak
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
【解】
touch mycp.c //新建文件
vim mycp.c //编辑文件
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
FILE *fp;
int fsize;
int fr;
char *buffer;
if(argc!=3)
{
printf("parameter error...\n");
exit(0);
}
else
{
fp = fopen(argv[1],"r");
if(!fp)
{
printf("can't open the file!\n");
exit(0);
}
fseek(fp,0,SEEK_END);
fsize = ftell(fp);
rewind(fp);
buffer=(char*)malloc((1+fsize)*sizeof(char));
if(!buffer)
{
printf("malloc error...\n");
exit(0);
}
fr = fread(buffer,1,fsize,fp);
if(!fr)
{
printf("read file error...\n");
exit(0);
}
fclose(fp);
fp=fopen(argv[2],"w");
if(!fp)
{
printf("open file error...\n");
exit(0);
}
fwrite(buffer,1,fsize,fp);
fclose(fp);
free(buffer);
return 0;
}
}
:wq 保存退出
cc mycp.c //编译
多进程题目
job3/mysys.c
实现函数mysys,用于执行一个系统命令,要求如下
mysys的功能与系统函数system相同,要求用进程管理相关系统调用自己实现一遍
使用fork/exec/wait系统调用实现mysys
不能通过调用系统函数system实现mysys
【解】
touch mysys.c //新建文件
vim mysys.c //编辑文件
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<stdlib.h>
void mysys(char *str)
{
pid_t pid;
if(str==NULL)
{
printf("shell string error...\n");
exit(0);
}
pid = fork();
if(pid==0)
{
execl("/bin/sh","sh","-c",str,NULL);
}
wait(NULL);
}
int main()
{
printf("----------------------------------------------------\n");
mysys("echo a b c d");
printf("----------------------------------------------------\n");
mysys("ls /");
printf("----------------------------------------------------\n");
return 0;
}
:wq 保存退出
cc mysys.c //编译
int main()
{
...
}
为测试程序,以下为mysys.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
void mysys(char *str)
{
pid_t pid;
if(str==NULL)
{
printf("shell string error...\n");
exit(0);
}
pid = fork();
if(pid==0)
{
execl("/bin/sh","sh","-c",str,NULL);
}
wait(NULL);
}
job3/sh1.c
该程序读取用户输入的命令,调用函数mysys(上一个作业)执行用户的命令,示例如下
#编译sh1.c
$ cc -o sh1 sh1.c
执行sh1
$ ./sh
sh1打印提示符>,同时读取用户输入的命令echo,并执行输出结果
echo a b c
a b c
sh1打印提示符>,同时读取用户输入的命令cat,并执行输出结果
cat /etc/passwd
root: x :0:0:root:/root:/bin/bash
daemon: x :1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin: x :2:2:bin:/bin:/usr/sbin/nologin
实现内置命令cd、pwd、exit
【解】
vim mysys.c
vim sh1.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<string.h>
extern void mysys(char *str);
int main()
{
while(1)
{
printf(">");
char str[100]={0};
char x;
int i=0;
do
{
x=getchar();
str[i]=x;
i++;
}while(x!='\n');
str[i-1]='\0';
if(str[0]=='e'&&str[1]=='x'&&str[2]=='i'&&str[3]=='t')
{
exit(0);
}
else if(str[0]=='c'&&str[1]=='d'&&str[2]==' ')
{
char cd[100]={0};
for(i=0;str[i+3]!='\0';i++)
{
cd[i]=str[i+3];
}
cd[i] = '\0';
chdir(cd);
// if(chdir(cd))
// {
// printf("cd:%s:no such directory\n",cd);
// }
}
mysys(str);
}
}
编译、链接
测试