20240103 (IO)作业

1>使用标佳io完成两个文件的拷贝

#include 
int main(int argc, const char *argv[])
{
	if(argc<3){
		printf("please enter at least 3 arguments..\n");
		printf("usage:./a.out  sourceFileName  destinationFileName\n");
		return -1;
	}

	FILE * sfp=NULL;
	FILE * dfp=NULL;

	if((sfp=fopen(argv[1],"r"))==NULL){
		perror("open source file error");
		return -1;
	}
	if((dfp=fopen(argv[2],"w"))==NULL){
		perror("open destination file error");
		return -1;
	}

	int res=0;
	char buf[20]="";
	while((res=fread(buf,1,sizeof(buf),sfp))!=0){
		fwrite(buf,1,res,dfp);
	}

	fclose(dfp);
	fclose(sfp);
	
	return 0;
}

2>使用文件IO完成两个文件的拷贝

#include 
#include 
#include 
#include 

int main(int argc, const char *argv[])
{
	if(argc<3){
		printf("please enter at least 3 arguments!\n");
		printf("uasage:./a.out  sourceFileName destinationFileName");
		return -1;
	}

	int sfi=-1;
	int dfi=-1;
	
	if((sfi=open(argv[1],O_RDONLY))==-1){
		perror("open source file error");
		return -1;
	}

	if((dfi=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664))==-1){
		perror("open destination file error");
		return -1;
	}

	char buf[20]="";
	int res=0;
	while((res=read(sfi,buf,sizeof(buf)))!=0){
		write(dfi,buf,res);
	}

	close(dfi);
	close(sfi);
	return 0;
}

3>将stat函数实现一遍

#include 
#include 
int main(int argc, const char *argv[])
{
	if(argc<2){
		printf("please enter at lease 2 arguments!\n");
		printf("usage:./a.out filename");
		return -1;
	}

	int fileInfoFlag=-1;
	struct stat fip;
	if((fileInfoFlag=stat(argv[1],&fip))==-1){
		perror("get file info error");
		return -1;
	}

//	printf("device ID=%ld\n",fip.st_dev);


	printf("device ID=%ld\t inode number=%ld\t file type=%#x\t file mode=%#o\n",fip.st_dev,fip.st_ino,fip.st_mode&S_IFMT,fip.st_mode&0777);


	return 0;
}

4>将目录操作实现一遍
 

#include 
#include 
int main(int argc, const char *argv[])
{
	if(argc!=2){
		printf("please enter enough arguments (2)\n");
		printf("usage:./a.out  filename\n");
		return -1;
	}

	DIR * dp=NULL;
	if((dp=opendir(argv[1]))==NULL){
		perror("open dir error");
		return -1;
	}

	struct dirent * dep=NULL;
	while((dep=readdir(dp))!=NULL){
		printf("ino=%ld\toffset=%ld\trecordlength=%ld\ttype=%ld\tfilename=%s\n",dep->d_ino,dep->d_off,dep->d_reclen,dep->d_type,dep->d_name);
	}
	return 0;
}

你可能感兴趣的:(jvm,算法)