首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请大家帮小弟我看看这段代码,为什么结果会有有关问题

2012-06-11 
【求助】请大家帮我看看这段代码,为什么结果会有问题?C/C++ code#includestdio.hvoid write(FILE *fpwrite

【求助】请大家帮我看看这段代码,为什么结果会有问题?

C/C++ code
#include<stdio.h>    void write(FILE *fpwrite, int n);//写入//void sort(FILE *fpread, FILE *to, int n);//排序//int main(){        FILE *fp1, *fp2;     int x;    fp1 = fopen("F:\\in.dat", "r");    fp2 = fopen("F:\\out.dat", "w");     if (fp1 == NULL || fp2 == NULL){         printf("Error!");         exit(0);    }    printf("Please input the count:");//输入数据的数量//     scanf("%d", &x);//从键盘读取数量//    write(fp1, x);// 在fp1里写入 x 个数据//     sort(fp1, fp2, x);//把fp1里的数据排序后,保存进fp2里//     fclose(fp1);     fclose(fp2);     getch();     return 0;}       void write(FILE *fpwrite, int n){        int i, a[1000];         printf("please input the number you want to write in file\n");     //读取n个数据,存进数组//    for (i = 0; i < n; i++)         scanf("%d", &a[i]);     //把这n个数组存进文件fpwrite里//    for (i = 0; i < n; i++)         fprintf(fpwrite, "%d\n", a[i]);}       void sort(FILE *read, FILE *to, int n){        int i, j, t, a[1000];     //从read文件内,读取n个数据//     for (i = 0; i < n; i++)         fscanf(read, "%d", a[i]);    //对从read里取出的数据进行排序//    for (i = 0; i < n; i++)        for (j = i + 1; j < n; j++){            if (a[i] > a[j]){                t =a[j];                a[j] = a[i];                a[i] = t;            }        }    //在文件to里输出最小的数据//    fprintf(to, "%d", a[0]);}   

我检查了很多次, 但是每次在in.dat里始终没有我所输入的数据, 但是在out.dat里有数据, 但是是个错误的数据。

[解决办法]
fp1 = fopen("F:\\in.dat", "r");是以读的形式打开的,怎么能在write中写数据呢?

热点排行
Bad Request.