文件操作举例
# 文件操作举例
# cat功能的实现
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int temp;
FILE *fpr;
if(argc==1)
{
printf("Parameter missing!\n");
exit(0);
}
if((fpr=fopen(argv[1],"r"))==NULL)
{
printf("Can not open the source file!\n");
exit(0);
}
fseek(fpr,0L,0);
while(!feof(fpr))
{
temp=fgetc(fpr);
putchar(temp);
}
fclose(fpr);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 删除文件功能的实现
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
int res;
if(argc==1)
{
printf("Parameter missing!\n");
exit(0);
}
res = remove( argv[1] );
if(res == 0)
{
printf("deleted!\n");
exit(0);
}
else if(res == -1)
{
printf("error happend!\n");
exit(0);
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cp功能的实现
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
char c;
FILE *fp1, *fp2;
if(argc<=2)
{
printf("Parameter missing!\n");
exit(0);
}
if((fp1=fopen(argv[1],"rb"))==NULL)
{
printf("Can not open the source file!\n");
exit(0);
}
if((fp2=fopen(argv[2],"a+"))==NULL)
{
printf("Can not establish the output file!\n");
exit(0);
}
fseek(fp1,0L,0);
fseek(fp2,0L,0);
while(!feof(fp1))
{
fread(&c, 1, 1, fp1);
fwrite(&c, 1, 1, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
编辑 (opens new window)
上次更新: 2023/02/18, 10:09:42
- 01
- Linux系统移植(五)--- 制作、烧录镜像并启动Linux02-05
- 03
- Linux系统移植(三)--- Linux kernel移植02-05