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);}
新闻热点
疑难解答