C语言文件处理

来源:互联网 发布:自选股软件下载 编辑:程序博客网 时间:2024/06/05 11:55

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);
}


0 0
原创粉丝点击