长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 文件操作

    • Linux文件属性
    • 文件操作
    • 文件操作举例
      • cat功能的实现
      • 删除文件功能的实现
      • cp功能的实现
    • 目录操作
    • 获得目录列表
    • 内存映射
    • 内存映射举例
  • Linux进程和线程

  • Linux信号

  • 进程间通信

  • Socket

  • C语言
  • 文件操作
DC Wang
2023-02-16
目录

文件操作举例

# 文件操作举例

# 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

# 删除文件功能的实现

#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

# 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
编辑 (opens new window)
#C#Linux#文件操作
上次更新: 2023/02/18, 10:09:42
文件操作
目录操作

← 文件操作 目录操作→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式