单选题有以下程序:#include main(){ FILE *fp; int i,a[6]={1,2,3,4,5,6},k; fp=fopen(data.dat,w+b); fwrite(a[0],sizeof(int),1,fp); for(i = 1;i 6;i++) { fseek(fp,0L,0); fread(k,sizeof(int),1,fp); fseek(fp,0L,0); a[i]+=k; fwrite(a[i],sizeof(int),1,fp); } rewind(fp); fread(k,sizeof(int),1,fp); fclose(fp); printf(%d,k);}程序的运行结果是( )。A21B6C123456D11
单选题
有以下程序:#include main(){ FILE *fp; int i,a[6]={1,2,3,4,5,6},k; fp=fopen(data.dat,w+b); fwrite(&a[0],sizeof(int),1,fp); for(i = 1;i < 6;i++) { fseek(fp,0L,0); fread(&k,sizeof(int),1,fp); fseek(fp,0L,0); a[i]+=k; fwrite(&a[i],sizeof(int),1,fp); } rewind(fp); fread(&k,sizeof(int),1,fp); fclose(fp); printf(%d,k);}程序的运行结果是( )。
A
21
B
6
C
123456
D
11
参考解析
解析:
“w+”表示打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失;若文件不存在则建立该文件;加入b字符后“w+b”用来告诉函数库打开的文件为二进制文件。程序执行过程为:以读/写方式打开一个新的二进制文件data.dat,从地址为a的数据块开始,一次输出一个整型字节的数据,只输出一次,将1写入文件中;执行for循环,将文件指针移到文件开头,将文件第一个数值读出赋给k,再将指针移动到开头,向文件内输入a[i]+k=3。for循环实现将a数组中元素累加,结果21存放在文件中。调用rewind将文件指针移动到开头,调用fread函数从文件中读出一个整型数据赋给k=21,调用fclose函数关闭文件,打印k值,答案选择A选项。
“w+”表示打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失;若文件不存在则建立该文件;加入b字符后“w+b”用来告诉函数库打开的文件为二进制文件。程序执行过程为:以读/写方式打开一个新的二进制文件data.dat,从地址为a的数据块开始,一次输出一个整型字节的数据,只输出一次,将1写入文件中;执行for循环,将文件指针移到文件开头,将文件第一个数值读出赋给k,再将指针移动到开头,向文件内输入a[i]+k=3。for循环实现将a数组中元素累加,结果21存放在文件中。调用rewind将文件指针移动到开头,调用fread函数从文件中读出一个整型数据赋给k=21,调用fclose函数关闭文件,打印k值,答案选择A选项。
相关考题:
有以下程序#include stdio.hmain ( ){ FILE *fp; int i,a[6]={1,2,3,4,5,6};fp=fopen("d3.dat", " w+b");fwrite(a,sizeof(int),6,fp);fseek(fp,sizeof(int)*3,SEEK_SET); /* 该语句使读文件的位置指针从文件头向后移动 3 个 int 型数据 */fread(a,sizeof(int),3,fp); fclose(fp);for(i=0;i6;i++) printf("%d,",a[i]);}程序运行后的输出结果是A)4,5,6,4,5,6,B)1,2,3,4,5,6,C)4,5,6,1,2,3,D)6,5,4,3,2,1,
有以下程序#include stido.hmain(){ FILE *fp; int a[10]={1,2,3,0,0},i;fp=fopen("d2.dat,"wb");fwrite(a,sizeof(int),5,fp);fwrite(a,sizeof(int),5,fp);fclose(fp);fp=fopen("d2.dat","rb");fread(a,sizeof(int),10,fp);fclose(fp);for(i=0;i10;i++) printf("%d",a[i]);}程序的运行结果是A)1,2,3,0,0,0,0,0,0,0,B)1,2,3,1,2,3,0,0,0,0,C)123,0,0,0,0,123,0,0,0,0,D)1,2,3,0,0,1,2,3,0,0,
有以下程序 #include main () {FILE *fp; int i,a[6]={1,2,3,4,5,6k}; fp=fopen(“d3.dat”,”w+b”); fwrite(a,sizeof(int),6,fp); fseek(fp,sizeof(int)*3,SEEK_SET);/*该语句使读文件的位置指针从文件头向后移动3个int型数据*/ fread(a,sizeof(int),3,fp); fclose(fp); for(i=0;i6;i++) printf(“%d,”,a[i]); }程序运行后的输出结果是( )。A.4,5,6,4,5,6,B.1,2,3,4,5,6,C.4,5,6,1,2,3,D.6,5,4,3,2,1,
有以下程序:#includestdio.hvoid main(){ fILE*fp; int i,a[4]={1,2,3,4},b; fp=fopen("data.dat","wb"); for(i=0;i4;i++) fwrite(a[i],sizeof(int),1,fp); fclose(fp) fp=fopen("data.dat","rb"); fseek(fp,-2L*sizeof(int),1,fp);/*文件中读取sizeof(int)字节的数据到变量b中*/ fclose(fp); printf("%d\n",b);}程序中fseek(fp,-2L*sizeof(int),SEEK_END);语句的作用是( )。A.使位置指针从文件末尾向前移2*sizeof(int)字节B.使位置指针从文件末尾向前移2字节C.使位置指针向文件末尾向前移懂2*sizeof(int)字节D.使位置指针向文件末尾移动2字节
有以下程序includemain(){FILE*fp;int i,a[6]={1,2,3,4,5,6};fp=fopen("d3.dat","w+b" 有以下程序 #include<stdio.h> main() {FILE*fp;int i,a[6]={1,2,3,4,5,6}; fp=fopen("d3.dat","w+b"); fwrite(a,sizeof(int),6,fp); fseek(fp,sizeof(int),3,SEEK_SET);/*该语句使读文件的位置指针从文件头向后移动3个int型数据*/ fread(a,sizeof(int),3,fp);fclose(fp); for(i=0A.4,5,6,4,5,6,B.1,2,3,4,5,6,C.4,5,6,1,2,3,D.6,5,4,3,2,1,
有以下程序 include main(){FILE *fp;int i; char ch[]="abcd",t; fp=fopen("abc.dat" 有以下程序 #include <stdio.h> main() {FILE *fp; int i; char ch[]="abcd",t; fp=fopen("abc.dat","wb++"); for(i=O;i<4;i++)fwrite(ch[i],1,1,fp); fseek (fp,-2L,SEEK_END); fread (t,1,1,fp); fclose (fp); printf ("%c\n",t); } 程序执行后的输出结果是______。A.dB.cC.bD.a
有以下程序 include main() { FILE *fp;int i,a[6]={1,2,3,4,5,6}; fp= 有以下程序 #include<stdio.h> main() { FILE *fp;int i,a[6]={1,2,3,4,5,6}; fp=fopen("d3.dat","w+b"); fwrite(a.sizeof(int),6,fp); fseek(fp,sizeof(int)*3,SEEK_SET):/*该语句使读文件的位置指针从文件头向后移动3个int型数据*/ fread(a,sizeof(int),3,fp);fclose(fp); for(i=0;i<6;i++)printf("%d,",a[i]); } 程序运行后的输出结果是A.4,5,6,4,5,6,B.1,2,3,4,5,6,C.4,5,6,1,2,3,D.6,5,4,3,2,1,
有以下程序#includestdio.hmain(){FILE *fp; int i,a[6]={l,2,3,4,5,6};fp=fopen("d3.dat","w+b");fwrite(a,sizeof(int),6,fp);fseek(fp,sizeof(int)*3,SEEK_SET);/*该语句使读文件的位置指针从文件头向后移动3个int型数据*/fread(a,sizeof(int),3,fp); fclose(fp);for(i=0;i6;i++)printf("%d,",a[i]);}程序运行后的输出结果是A.4,5,6,4,5,6,B.1,2,3,4,5,6,C.4,5,6,1,2,3,D.6,5,4,3,2,1,
有以下程序(提示:程序中fseek(fp-2L*sizeof(int),SEEK_END) ;语句的作用是使位置指针从文件末尾向前移2*sizeof(ing)字节) #include <stdio.h> main() { FILE *fp; int i, a[4]={1,2,3,4},b; fp=fopen("data.dat","wb"); for(i=0;i<4;i++) fwrite(a[i],sizeof(int),1,fp); fclose(fp); fp=fopen("data.dat","rb"); fseek(fp,-2L*sizeof(int),SEEK_END) ; fread (b, sizeof (int),1,fp); /*从文件中读取sizeof(int)字节的数据到变量b中*/ fclose(fp); printf("%d\n",B) ; } 执行后输出结果是A.2B.1C.4D.3
以下程序运行后的输出结果是 。includemain(){FILE*fp;int x[6]={l,2,3,4,5,6},i;fp=fopen(“test.dat”,”wb“);fwrite(x,sizeof(int),3,fp);rewind(fp);fread(x,sizeof(int),3,f}]);for(i=0;i6;i++)printf(“%d”,x[i]);printf(“\n”);fclose(fp);}
有以下程序:include main(){FILE*fp;int i,k,n; fp=fopen("data.dar","w+");for(i=1;i 有以下程序: #include <stdio.h> main() { FILE *fp; int i,k,n; fp=fopen("data.dar","w+"); for(i=1;i<6;i++) { fprintf(fp,"%d ",i); if(i%3==0) fprintf(fp,"\n"); } rewind(fp); fscanf(fp,"%d%d",k,n); printf("%d%d\n",k,n); fclose(fp); } 程序运行后的输出结果是( )。A.0 0B.123 45C.1 4D.1 2
有以下程序(提示:程序中fseek(fp,-2L*sizeof(int),SEEK_END);语句的作用是使位置指针从文件末尾向前移2 * sizeof(int)字节): #include <stdio.h> main() { FILE *fp; int i, a[4]={1,2,3,4},b; fp=fopen("data.dar","wb"); for(i=0;i<4;i++) fwrite(a[i],sizeof(int),1,fp); fclose(fp); fp=fopen("data.dar","rb"); fseek(fp,-2L*sizeof(int),SEEK_END); fread(b,sizeof(int),1,fp); /*从文件中读取sizeof(int)字节的数据到变量b中*/ fclose(fp); printf("%d\n",b); } 执行后的输出结果( )。A.2B.1C.4D.3
有以下程序 include main() { FILE *fp; int' a[10]={1,2,3,0,0},i; 有以下程序 #include <stdio.h> main() { FILE *fp; int' a[10]={1,2,3,0,0},i; fp = fopen("d2.dat", "wb"); fwrite(a, sizeof(int), 5, fp); fwrite(a, sizeof(int), 5, fp); fclose(fp); fp = fopenCd2.dat", "rb"); fread(a, sizeof(int), 10, fp); fclose(fp); for (i=0; i<10; i++) printf("%d,", a[i]); }A.1,2,3,0,0,0,0,0,0,0,B.1,2,3,1,2,3,0,0,0,0,C.123,0,0,0,0,123,0,0,0,0,D.1,2,3,0,0,1,2,3,0,0,
有以下程序:include main( ){FILE * fp; int i,k,n;fp = fopen( "data. dat" ,"w +" ) 有以下程序:#include <stdlo.h>main( ){ FILE * fp; int i,k,n; fp = fopen( "data. dat" ,"w +" ) for(i = 1 ;i<6;i ++ ) { fprintf(fp."% d",i); if(i%3 ==0)fprintf(fp," \n"); } rewind(fp); fscanf(fp." % d% d" ,k, n) ;printf(" % d%d \n" ,k,n); fclose(fp);A.0 0B.123 45C.1 4D.1
有以下程序: include main() {FILE *fp; int i,k,n; fp=fopen("data 有以下程序: #include <stdio.h> main() {FILE *fp; int i,k,n; fp=fopen("data.dat","w+"); for(i=1;i<6;i++) {fprintf(fp,"%d ",i); if(i%3==0) fprintf(fp,"\n"); } rewind(fp); fscanf(fp,"%d%d",k,n); printf("%d %d\n",k,n); fclose(fp); } 程序运行后的输出结果是 ______。A.0 0B.123 45C.1 4D.1 2
有以下程序(提示:程序中fseek(fp,-2L*sizeof(int),SEEK_END);语句的作用是使位置指针从文件尾向前移 2*sizeof(int)字节) #include <stdio.h> main( ) { FILE *fp; int i,a[4]={1,2,3,4},b; fp=fopen("data.dat","wb"); for(i=0;i4;i++) fwrite(a[i],sizeof(int),1,fp); fclose(fp); fp=fopen("data.dat","rb"); fseek(fp,-2L*sizeof(int).SEEK_END); fread(b,sizeof(int),1,fp);/*从文件中读取sizeof(int)字节的数据到变量b中*/ fclose(fp); printf("%d\n",b);} 执行后输出结果是( )A.2B.1C.4D.3
有以下程序(提示,程序中fseek(fp,-2L*sizeof(int),SEEK_END);语句的作用是使位置指针从文件末尾向前移2*sizeof(int)字节): #include <stdio.h> main() { FILE *fp; int i, a[4]={l,2,3,4},b; fp=fopen("data.dat","wb"); for(i=0;i<4;i++) fwrite(a[i],sizeof(int),1,fp); fclose(fp); fp=fopen("data.dar","rb"); fseek(fp,-2L*sizeof(int),SEEK END); fread(b,sizeof(int),1,fp);/*从文件中读取sizeof(int)字节的数据到变量b中 */ fclose(fp); printf("%d\n",b); } 执行后输出结果是( )。A.2B.1C.4D.3
有以下程序:includemain(){ FILE *fp;int a[10]={1,2,3,0,0},i;fp=fopen("d2.dat","wb 有以下程序: #include<stdio.h> main() { FILE *fp;int a[10]={1,2,3,0,0},i; fp=fopen("d2.dat","wb"); fwrite(a,sizeof(int),5,fp); fwrite(a,sizeof(int),5,fp); fclose(fp); fp=fopen("d2.dat","rb"); fread(a,sizeof(int),10,fp); fclose(fp); for(i=0;i<10;i++)printf("%d,",a[i]); } 程序的运行结果是( )。A.1,2,3,0,0,0,0,0,0,0,B.1,2,3,1,2,3,0,0,0,0,C.123,0,0,0,0,123,0,0,0,0,D.1,2,3,0,0,1,2,3,0,0,
有以下程序 #includestdi0.h main {FILE*fp;int a[0]一{1,2,3,0,0},i; fp=fopen("d2.dat,"wb"); fwrite(a,sizeof(int),5,fp); fwrite(a,sizeof(int),5,fp); fclose(fp); fp=fopen("d2.dat","rb"); fread(a.sizeof(int),10,fp); fclose(fp); for(i=0;i10;i++)printft("%d",a[i]); } 程序的运行结果是( )。A.1,2,3,0,0,o,0,0,0,0,B.1,2,3,1,2,3,0,0,0,0,C.123,0,0,0,0,123,0,0,0,0,D.1,2,3,0,0,1,2,3,0,0,
有以下程序#includemain{ FILE *fp;int a[10]={1,2,3,0,0},i;fp=fopen(d2.dat,wb);fwrite(a,sizeof(int),5,fp);fwrite(a,sizeof(int),5,fp);fclose(fp);fp=fopen(d2.dat,rb);fread(a.sizeof(int),10,fp);fclose(fp);for(i=0;i10;i++) printft(%d,a[i]);}程序的运行结果是( )。A.1,2,3,0,0,0,0,0,0,0,B.1,2,3,1,2,3,0,0,0,0,C.123,0,0,0,0,123,0,0,0,0,D.1,2,3,0,0,1,2,3,0,0,
有以下程序:include main(){FILE *fp;int i,k=0, n=0; fp=fopen("d1.dat","w"); for(i 有以下程序: #include <stdio.h> main() { FILE *fp; int i,k=0, n=0; fp=fopen("d1.dat","w"); for(i=1;i<4;i++) fprintf(fp,"%d",i); fclose(fp); fp=fopen("d1.dat","r"); fscanf(fp,"%d%d",k,n); printf("%d%d\n",k,n); fclose(fp); } 执行后输出结果是( )。A.1 2B.123 0C.1 23D.0 0
有以下程序 include main( ) { FILE *fp;int i,a[6]={1,2,3,4,5,6}; fp=fopen("d3.da 有以下程序#include <stdio.h>main( ){ FILE *fp;int i,a[6]={1,2,3,4,5,6};fp=fopen("d3.dat","w+b");fwrite(a,sizeof(int),6,fp);fseek(fp,sizeof(int)*3,SEEK_SET);/*该语句使读文件的位置指针从文件头向后移动3个int型数据*/fread(a,sizeof(int),3,fp);fclose(fp);for(i=0;i<6;i++)printf("%d,",a[i]);}程序运行后的输出结果是A.4,5,6,4,5,6,B.1,2,3,4,5,6,C.4,5,6,1,2,3,D.6,5,4,3,2,1,
有以下程序#includestdio.hmain(){FILE *fp; it a[10]={1,2,3,0,0};fp=fopen(“d2.dat”,”wb”);fwrite(a,sizeof(int),5,fp);fwrite(a,sizeof(int),5,fp);fclose(fp);fp=fopen(“d2.dat”,”rb”);fread(a,sizeof(int),10,fp);for(i=0;i10;i++) printf(“%d”,A.;}程序运行结果是( )。
(15)以下程序运行后的输出结果是 【15】 。#include stdio.hmain(){ FILE *fp; int x[6]={1,2,3,4,5,6},i;fp=fopen("test.dat","wb");fwrite(x,sizeof(int),3,fp);rewind(fp);fread(x,sizeof(int),3,fp);for(i=0;i6;i++) printf("%d",x[i]);printf("\n");fclose(fp);}
单选题有以下程序:#include main(){ FILE *fp; int i,a[6]={1,2,3,4,5,6},k; fp=fopen(data.dat,w+b); fprintf(fp,%d,a[0]); for(i = 1;i 6;i++) { rewind(fp); fprintf(fp,%d,a[i]); } rewind(fp); fscanf(fp,%d,k); fclose(fp); printf(%d,k);}程序运行后的输出结果是( )。A6B21C123456D654321
单选题有以下程序:#include main(){ FILE *fp; int i,a[6]={1,2,3,4,5,6},k; fp=fopen(data.dat,w+b); fwrite(a[0],sizeof(int),1,fp); for(i = 1;i 6;i++) { fseek(fp,0L,0); fread(k,sizeof(int),1,fp); fseek(fp,0L,0); a[i]+=k; fwrite(a[i],sizeof(int),1,fp); } rewind(fp); fread(k,sizeof(int),1,fp); fclose(fp); printf(%d,k);}程序的运行结果是( )。A21B6C123456D11
单选题有以下程序#include main(){ FILE *fp; int a[10]={1,2,3,0,0},i; fp=fopen(d2.dat,wb); fwrite(a,sizeof(int),5,fp); fwrite(a,sizeof(int),5,fp); fclose(fp); fp=fopen(d2.dat,rb); fread(a,sizeof(int),10,fp); fclose(fp); for(i=0;i10;i++) printf(%d,,a[i]);}程序的运行结果是( )。A1,2,3,0,0,0,0,0,0,0,B1,2,3,1,2,3,0,0,0,0,C123,0,0,0,0,123,0,0,0,0,D1,2,3,0,0,1,2,3,0,0,
单选题有以下程序:#include main(){ FILE *fp; int i,a[6]={1,2,3,4,5,6},k; fp=fopen(data.dat,w+b); fprintf(fp,%d,a[0]); for(i = 1;i 6;i++) { fseek(fp,0L,0); fscanf(fp,%d,k); fseek(fp,0L,0); fprintf(fp,%d,a[i]+=k); } rewind(fp); fscanf(fp,%d,k); fclose(fp); printf(%d,k);}程序的运行结果是( )。A21B6C123456D11