实验五(5) Linux C文件系统与文件操作(笔记)

这篇博客详细介绍了Linux环境下C语言进行文件系统和文件操作的实验内容,包括文件的打开、读写、权限修改、文件信息获取等。通过一系列的C语言程序实例,如文件内容转换、文件重命名、权限设置等,阐述了如何使用stat函数获取文件信息,以及各种文件操作的错误代码。此外,还涵盖了进程相关的实验,涉及用户标识、fork、vfork、exec函数族、exit函数、kill函数和信号处理等知识点。
摘要由CSDN通过智能技术生成

实验五(一) Linux C文件系统与文件操作(笔记)

一 、实验目的:
1.掌握文件以及缓冲文件系统、文件指针的概念;
2.学会使用文件打开、关闭、读、写等文件操作函数;
3.学会用缓冲文件系统对文件进行简单的操作;
4.非缓冲文件的操作。

二、实验设备:
1.硬件 PC机
2.软件 VMware Workstation、Linux

三、实验内容:
编写程序并上机调试运行。
1.新建一个源文件如:f1.c编程实现:打开a.txt文本文件,读取其中内容,将其中小写字母转换为大写字母后,复制到b.txt新建文件中。

#include <stdio.h>
#include <ctype.h>
int main(){
char ch;
FILE *fp1,*fp2;
if((fp1=fopen("a.txt","r"))==NULL){
	printf("file open error!\n");
	exit(0);
}
if((fp2=fopen("b.txt","w"))==NULL){
	printf("file open error!\n");
	exit(0);
}
while((ch=fgetc(fp1))!=EOF){
	if(isalpha(ch) && islower(ch))
		ch=ch-0x20;
	fputc(ch,fp2);
}  
fclose(fp1);
fclose(fp2);
return 0; 	
}

2.新建一个源文件如:f2.c编程实现:程序打开某个文本文件,然后把此文件中的小写字母转换为大写字母其他字符不变,其中文件名作为命令行参数。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#define PERMS 0666
#define DUMMY 0
#define BUFSIZE 1024
int main(int argc,char *argv[]){
int fd,num,i,num2;
char iobuffer[BUFSIZE];
if(argc!=2){
	printf("file error!\n");
	return 1;  
}
if((fd=open(*(argv+1),O_RDWR,PERMS))==-1){
	printf("Source file open error!\n");
	return 2;
} 
num=read(fd,iobuffer,BUFSIZE);
for(i=0;i<num;i++){
	if(iobuffer[i]>=97&&iobuffer[i]<=122)
		iobuffer[i]=iobuffer[i]-32;
}
lseek(fd,0,SEEK_SET);
num2=write(fd,iobuffer,num);
if(num2!=num){
	printf("write error!\n");
	return 3;
}
close(fd);
return 0;
}

3.新建一个源文件如:f3.c编程实现:程序从键盘接收多个字符,直到输入#字符,输入结束,然后,程序把这些输入的字符写入文件 file.txt中。

#include<stdio.h>
#include<stdlib.h>
int main(){   
FILE *fp;
char ch;
if((fp=fopen("file.txt","w"))==NULL){
	printf("cannot open this file\n");
	exit(0);
}
else{
	printf("file open successful--writeonly\n");
}
printf("Input character from keybord and end input with charater'#'\n");
while(1){
	ch=getchar();
	//填空
	//填空
}
fputc(0x0d,fp);
fputc(0x0a,fp);
if(fclose(fp)==0){
	printf("file write successful and closed\n");
}
if((fp=fopen("file.txt","r"))==NULL){
	printf("cannot open this file\n");
	exit(0);
}
else{
	printf("file open successful--readonly\n");
}
printf("Input character from keybord and end input with charater'#'\n");
while(1){
	ch=fgetc(fp);
	if(ch!=EOF)  
		printf("%c",ch);
	else 
		break;
}
if(fclose(fp)==0){
	printf("file read  sucessful and closed\n");
}
return 0;
}

4.编写一个程序f4.c,实现文件改名操作。

#include <stdio.h>
int main(int argc,char **argv){
if(argc<3){
	printf("Usage: %s old_name new_name\n",argv[0]);
	return 0;
}
printf("%s=>%s",argv[1],argv[2]);
if(rename(argv[1],argv[2])<0)
	printf("error!\n");
else
	printf("ok!\n");
return 0;
}
  1. 编写一个程序f5.c,实现改变文件拥有者。

     #include<sys/types.h>
     #include<unistd.h>
     main(){
     	chown("a.txt",0,0); 
     } 
    
  2. 编写一个程序f6.c,实现修改文件的访问权限

    #include<sys/types.h> 
     	#include<sys/stat.h> 
     	main(){ 
     		chmod("a.txt",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 
     	}
    
  3. 编写一个程序f7.c,读取一个目录中的内容。(修改代码,执行时读取指定的某个目录)

    #include <dirent.h>
     struct dirent *readdir(DIR *dir);
     struct dirent {
     	long d_ino;//文件Inode号
     	off_td_off;//文件在目录文件中位置偏移
     	unsigned short int d_reclen;//文件名长
     	unsigned char d_type;//文件类型
     	char d_name[256];//文件名
     };
    

程序代码:

#include<sys/types.h>  
#include<dirent.h>  
#include<unistd.h> 
main(){  
DIR * dir; 
struct dirent * ptr; 
int i; 
dir =opendir("/etc/rc.d"); 
while((ptr = readdir(dir))!=NULL) {
	printf("d_name: %s\n",ptr->d_name); 
} 
closedir(dir); 
}  
  1. 编写一个程序f8.c,读取一个目录中的内容及其子目录中的内容。(修改代码,执行时读取指定的某个目录)

     # include <stdio.h>
     # include <string.h>
     # include <stdlib.h>
     # include <unistd.h>
     # include <dirent.h>
     # include <sys/types.h>
     int re_readdir(char name[]){
     char str[1000] = {0};
     strcpy(str, name);
     printf("%s\n", str);
     DIR *dir = opendir(str);
     if(NULL == dir){
     	perror("opendir");
     	return -1;
     }
     struct dirent *ent = readdir(dir);
     while(NULL != ent){
     	strcpy(str,name);
     	printf("name = %s: type &#
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值