西北农林科技大学 c语言上机 实习8答案_c语言上机实习报告

实习报告 时间:2020-02-28 18:35:24 收藏本文下载本文
【www.daodoc.com - 实习报告】

西北农林科技大学 c语言上机 实习8答案由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言上机实习报告”。

1.文本文件中字符个数统计 #include #include int main(void){

char ch, filename[200];

int count = 0;

FILE *fp;

scanf(“%s”, filename);

if((fp = fopen(filename, “r”))== NULL)

{

printf(“File open error!n”);

exit(1);

}

while((ch = fgetc(fp))!= EOF)

{

count++;

}

printf(“%dn”,count);

if(fclose(fp))

{

printf(“File close error!n”);

exit(1);

}

return 0;}

2.文件中数据的排序 #include #include int main(void){

int a[10], temp = 0, i = 0, j = 0, k = 0;

char filename1[80], filename2[80];

FILE *fp1,*fp2;

scanf(“%s”, filename1);

scanf(“%s”, filename2);

if((fp1 = fopen(filename1, “rb”))== NULL)

{

printf(“Input file open error!n”);

exit(1);

}

if((fp2 = fopen(filename2,“wb”))== NULL)

{

printf(“Output file create error!n”);

exit(1);

}

for(i = 0;i

{

fread(&a[i], sizeof(int), 1, fp1);

/* 读出数据 */

}

for(i = 0;i

{

k = i;

for(j = i + 1;j

if(a[k] > a[j])k = j;

if(k!= i)

{

temp = a[k];

a[k] = a[i];

a[i] = temp;

}

} for(i = 0;i

/*

{

fwrite(&a[i], sizeof(int), 1, fp2);

}

if(fclose(fp1))

{

printf(“Input file close error!n”);

exit(1);

}

if(fclose(fp2))

{

printf(“Output file close error!n”);

exit(1);

}

/*验证结果的代码,请不要改动*/

if((fp1 = fopen(filename2, “rb”))== NULL)

{

printf(“Result file open error!n”);

exit(1);

}

/* 排序 */ 写入数据 */

for(i = 0;i

{

fread(&a[i], sizeof(int), 1, fp1);

/* 读出数据 */

}

if(fclose(fp1))

{

printf(“Result file close error!n”);

exit(1);

}

for(i = 0;i

printf(“%d ”, a[i]);

printf(“%dn”, a[i]);

return 0;} 3 二进制数据文件/ #include #include int main(void){

int a[10], temp = 0, i = 0, j = 0, k = 0;

char filename1[80], filename2[80];

FILE *fp1,*fp2;

scanf(“%s”, filename1);

scanf(“%s”, filename2);

if((fp1 = fopen(filename1, “r”))== NULL)

{

printf(“Input file open error!n”);

exit(1);

}

if((fp2 = fopen(filename2, “wb”))== NULL)

{

printf(“Output file open error!n”);

exit(1);

}

for(i = 0;i

{

fscanf(fp1, “%d”, &a[i]);

fwrite(&a[i], sizeof(int), 1, fp2);

}

if(fclose(fp1))

{

printf(“Input file close error!n”);

exit(1);

}

if(fclose(fp2))

{

printf(“Output file close error!n”);

exit(1);

}

if((fp1 = fopen(filename2, “rb”))== NULL)

{

printf(“Result file open error!n”);

exit(1);

}

for(i = 0;i

{

fread(&a[i], sizeof(int), 1, fp1);

}

if(fclose(fp1))

{

printf(“Result file close error!n”);

exit(1);

}

for(i = 0;i

printf(“%d ”, a[i]);

printf(“%dn”, a[i]);

return 0;}

4.比较2个文本文件的内容 #include #include int main(void){

int i = 1, flag = 0;

char filename1[80], filename2[80];

FILE *fp1, *fp2;

/* 读出数据 */

scanf(“%s”, filename1);

scanf(“%s”, filename2);

if((fp1 = fopen(filename1, “r”))== NULL)

{

printf(“Input file1 open error!n”);

exit(1);

}

if((fp2 = fopen(filename2,“r”))== NULL)

{

printf(“Input file2 open error!n”);

exit(1);

}

while(!feof(fp1))

{

if(fgetc(fp1)== fgetc(fp2))

i++;

else

{

flag = 1;

break;

}

}

if(flag)

printf(“%dn”, i);

else

printf(“is equaln”);if(fclose(fp1))

{

printf(“Input file1 close error!n”);

exit(1);

}

if(fclose(fp2))

{

printf(“Input file2 close error!n”);

exit(1);

}

return 0;}

下载西北农林科技大学 c语言上机 实习8答案word格式文档
下载西北农林科技大学 c语言上机 实习8答案.doc
将本文档下载到自己电脑,方便修改和收藏。
点此处下载文档

文档为doc格式

    热门文章
      整站推荐
        点击下载本文