首页 > 学院 > 开发设计 > 正文

C语言文件处理

2019-11-06 07:03:11
字体:
来源:转载
供稿:网友

2011年,第一次考对文件的操作,之后2016年也考了对文件的操作

题目:读入一个文件,对读入的字符串按字符大小排序后,输出到另外一个文件

/*读入一个文件,对读入的字符串按字符大小排序后,输出到另外一个文件*/#include<stdio.h>#include<stdlib.h>int cmp(const void *a, const void *b){	return *(char *)a - *(char *)b;}int main(){	FILE *in, *out;	char inFileName[20] = "in.txt";	char outFileName[20] = "out.txt";	if ((in = fopen(inFileName, "r")) == NULL){		PRintf("cannot open %s/n", inFileName);		exit(0);	}	if ((out = fopen(outFileName, "w+")) != NULL){//		printf("cannot open %s/n", outFileName);//		exit(0);	}	char str[80];	int length=0;	while (!feof(in)){		fscanf(in, "%s", str);				fprintf(out, "%s", str);			}	while (str[length++] != '/0');	length = length - 1;	qsort(str, length, sizeof(str[0]), cmp);	printf("%s", str);fclose(in);fclose(out);
}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表