实验五(一) 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;
}
-
编写一个程序f5.c,实现改变文件拥有者。
#include<sys/types.h> #include<unistd.h> main(){ chown("a.txt",0,0); }
-
编写一个程序f6.c,实现修改文件的访问权限
#include<sys/types.h> #include<sys/stat.h> main(){ chmod("a.txt",S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); }
-
编写一个程序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);
}
-
编写一个程序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 &#