现在的位置: 首页 > 综合 > 正文

c语言之文件操作练习

2013年10月08日 ⁄ 综合 ⁄ 共 6492字 ⁄ 字号 评论关闭

之前操作系统作业有个题目涉及到文件操作,在osjob分类下面,做得比较简单,并没有完全按照老师的要求来做。

程序里面需要设定传入参数,我是直接在vc工程里面设置的,project->settings->Debug选项卡下面有个program arguments,在这个输入框里面输入参数,多个参数以空格分开

/************************************************************************/
/* 01.c																	*/	
/* 读取磁盘文件内容                                                     */
/************************************************************************/
#include <stdio.h>
int main(int argc,char *argv[])
{
	FILE *fp;//定义文件指针fp
	char ch,*fileName;
	fileName = argv[1];
	fp = fopen(fileName,"r");//只读方式
	if(fp == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	ch = fgetc(fp);
	while (ch != EOF)
	{
		putchar(ch);//将读入的字符输出到屏幕上
		ch = fgetc(fp);
	}
	printf("\n");
	fclose(fp);//关闭文件
	return 0;
}

/************************************************************************/
/* 02.c																	*/	
/* 将数据写入磁盘文件                                                   */
/************************************************************************/
#include <stdio.h>
int main(int argc,char *argv[])
{
	FILE *fp;
	char ch,*fileName;
	fileName = argv[1];
	fp = fopen(fileName,"w");
	if(fp == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	ch = getchar();//读取屏幕输入字符
	while (ch != '#')
	{
		fputc(ch,fp);//将读入的字符写到磁盘文件上去
		ch = getchar();
	}
	fclose(fp);
	return 0;
}

/************************************************************************/
/* 03.c																	*/	
/* 格式化读写文件														*/
/************************************************************************/
//fprintf和fscanf函数的读写对象不是终端而是磁盘文件
#include <stdio.h>
int main(int argc,char *argv[])
{
	int i;
	char str[50],*fileName;
	fileName = argv[1];
	FILE *fp;
	if((fp = fopen(fileName,"w")) == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	printf("Input the string:");
	scanf("%s",str);
	fprintf(fp,"%s",str);//将str字符串内容以%s形式写到fp所指文件上
	fclose(fp);
	fp = fopen(fileName,"r");//重新以只读方式打开磁盘文件
	while (fscanf(fp,"%s",str) != EOF)//从fp所指的文件中以%s形式读入字符串
	{
		for (i =0; str[i] != '\0'; i++)
		{
			if(str[i] >= 'a' && str[i] <= 'z')//将小写转换成大写
				str[i] = str[i] - 32;
		}
		printf("%s\n",str);
	}
	fclose(fp);
	return 0;
}

/************************************************************************/
/* 04.c																	*/	
/* 成块读写操作															*/
/* 键盘输入学生成绩信息,保存到指定文件,再输出到屏幕上					*/
/* fwrite(buff,size,count,fp);buff是一个指针,是要输出数据的地址        */
/*	size要读写的字节数;count要进行读写多少个size字节的数据项			*/
/************************************************************************/
#include <stdio.h>
#define STUNUM 5
struct stuScore
{
	char name[10];//姓名
	int num;//学号
	int math;//数学成绩
	int china;//语文
}stu[STUNUM];
//保存到磁盘
int save(char *fileName,int n)
{
	FILE *fp;
	int i;
	if((fp=fopen(fileName,"w")) == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	for (i=0; i<n; i++)
	{
		//将一组数据输出到fp所指的文件中
		if(fwrite(&stu[i],sizeof(struct stuScore),1,fp) != 1)
		{
			printf("write error\n");
		}
	}
	fclose(fp);
	return 0;
}
//从磁盘读取
int show(char *fileName,int n)
{
	FILE *fp;
	int i;
	if((fp=fopen(fileName,"r")) == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	for (i=0; i<n; i++)
	{
		//从fp所指的文件中读取数据存放到stu中
		fread(&stu[i],sizeof(struct stuScore),1,fp);
		printf("%-10s%4d%4d%4d\n",stu[i].name,stu[i].num,stu[i].math,stu[i].china);
	}
	fclose(fp);
	return 0;
}
int main(int argc,char *argv[])
{
	char *fileName;
	int i;
	fileName = argv[1];
	printf("Input the stuname stunum mathscore chinascore\n");
	for(i=0 ;i<STUNUM ;i++)
	{
		printf("NO%d.",i+1);
		scanf("%s%d%d%d",stu[i].name,&stu[i].num,&stu[i].math,&stu[i].china);
		save(fileName,STUNUM);
	}
	show(fileName,STUNUM);
	return 0;
}


/************************************************************************/
/* 05.c																	*/
/* 文件合并																*/
/************************************************************************/
#include <stdio.h>
int main(int argc,char *argv[])
{
	//将file2内容合并到file1
	char *fileName1,*fileName2;
	char ch;
	fileName1 = argv[1];
	fileName2 = argv[2];
	FILE *fp1;
	FILE *fp2;
	if((fp1=fopen(fileName1,"a+")) == NULL)
	{
		printf("file1 open error\n");
		return -1;
	}
	printf("file1:\n");
	ch = fgetc(fp1);
	while (ch != EOF )
	{
		putchar(ch);
		ch = fgetc(fp1);
	}
	printf("\n");
	if((fp2=fopen(fileName2,"r")) == NULL)
	{
		printf("file2 open error\n");
		return -1;
	}
	printf("file2:\n");
	ch = fgetc(fp2);
	while (ch != EOF )
	{
		putchar(ch);
		ch = fgetc(fp2);
	}
	printf("\n");
	fseek(fp2,0,0);//将文件2指针从文件最后移到文件2首部
	while ((ch = fgetc(fp2)) != EOF)
	{
		fputc(ch,fp1);//将文件2中的内容写到文件1末尾
	}
//	fp1 = fopen(fileName1,"r");
//	ch = fgetc(fp1);
//	while (ch != EOF)
//	{
//		putchar(ch);
//		ch = fgetc(fp1);
//	}
	printf("\nMerge Success\n");
	fclose(fp2);
	fclose(fp1);
	return 0;
}

/************************************************************************/
/* 06.c																	*/
/* 文件内容加密                                                         */
/************************************************************************/
#include <stdio.h>
//加密
int code(char *sourcefile,char *pwd,char *codefile,char *decodefile)
{
	FILE *fp1;
	FILE *fp2;
	FILE *fp3;
	char ch;
	int i=0;
	if((fp1=fopen(sourcefile,"rb"))==NULL)
	{
		printf("open sourcefile error\n");
		return -1;
	}
	if((fp2=fopen(codefile,"wb"))==NULL)
	{
		printf("open codefile error\n");
		return -1;
	}
	if((fp3=fopen(decodefile,"wb"))==NULL)
	{
		printf("open decodefile error\n");
		return -1;
	}
	ch = fgetc(fp1);
	while ( !feof(fp1))
	{
		ch = ch ^ *(pwd+i);//与密钥异或方式加密
		fputc(ch,fp2);
		ch = ch ^ *(pwd+i);//解密----再异或一次
		fputc(ch,fp3);
		ch = fgetc(fp1);	
		i++;
		if(i > 10)
			i = 0;
	}
	fputc('\0',fp2);
	return 0;
	
}
int main(int argc,char *argv[])
{
	int re;
	char *sourcefile,*pwd,*codefile,*decodefile;
	sourcefile = argv[1];//源文件
	pwd = argv[2];//密钥
	codefile = argv[3];//编码后生成文件
	decodefile = argv[4];//解码后生成文件

	printf("coding begin...\n");
	if(re =code(sourcefile,pwd,codefile,decodefile) != -1)
		printf("coding...\n");
	else
		printf("coding error\n");
	printf("coding success\n");
	return 0;
}
/************************************************************************/
/* 07.c																	*/
/* 删除文件中的记录                                                     */
/************************************************************************/
#include <stdio.h>
#include <string.h>
#define STUSIZE 5
struct student{
	char name[10];
	int score;
}stu[STUSIZE];
//将读入的学生数据写到磁盘文件上
int write(struct student stu[],char * filename,int n)
{
	FILE *fp1;
	int i;
	if((fp1=fopen(filename,"wb") )== NULL)//以追加的方式打开指定文件
	{
		printf("open file error\n");
		return -1;
	}
	for (i=0 ;i<n; i++)
	{//将学生信息输出到磁盘文件上
		if(fwrite(&stu[i],sizeof(struct student),1,fp1) != 1)
			printf("write error\n");
	}	
	fclose(fp1);
	return 0;
}
//显示文件内容
int show(struct student stu[],char *filename,int &n)
{
	FILE *fp2;
	int i;
	if ((fp2=fopen(filename,"rb")) == NULL)
	{
		printf("open file error\n");
		return -1;
	}
	for (i=0; i<n; i++)
		//读取磁盘文件上的信息到stu数组中
		if(fread(&stu[i],sizeof(struct student),1,fp2) !=0)
			printf("\n %8s%7d\n",stu[i].name,stu[i].score);
	n = i;
	fclose(fp2);
	return 0;
}
//删除操作
int deleteData(struct student stu[],char *filename,int n)
{
	char name[10];
	int i,j,flag=1;
	printf("\n Input name which do you want to delete:");
	scanf("%s",name);
	for (i=0; flag && i<n; i++)
	{
		if(strcmp(name,stu[i].name) == 0)//查找与输入姓名相匹配的位置
		{
			for(j=i; j<n-1; j++)
			{//查找到要删除的位置后将后面的信息前移覆盖删除位置
				strcpy(stu[j].name,stu[j+1].name);
				stu[j].score = stu[j+1].score;
			}
			flag = 0;
		}
	}
	if (!flag)
	{
		n = n-1;
	}
	else
		printf("\nNot find the same name infomation\n");
	write(stu,filename,n);
	return 0;
}
int main(int argc,char *argv[])
{
	char *filename;
	int i,n=STUSIZE;
	filename = argv[1];
	for (i=0; i<STUSIZE; i++)
	{
		printf("NO%d.",i+1);
		scanf("%s%d",stu[i].name,&stu[i].score);
	}
	write(stu,filename,STUSIZE);
	printf("orignal data:\n");
	show(stu,filename,n);
	deleteData(stu,filename,STUSIZE);
	printf("changed data:\n");
	show(stu,filename,n);//删除一条数据后,人数-1
	return 0;
}

抱歉!评论已关闭.