向磁盘文件18-1.txt中输入一行字符。
#include "stdio.h"
#include "stdlib.h"
main()
{【1】 *fp;
char ch;
if((fp=fopen("18-1.txt","w"))==NULL)
{ printf("Cannot open.
");
exit(0);
}
while((ch=getchar())!='
')
fputc( 【2】 ,fp);
【3】 ;
}
#include "stdio.h"
#include "stdlib.h"
main()
{【1】 *fp;
char ch;
if((fp=fopen("18-1.txt","w"))==NULL)
{ printf("Cannot open.
");
exit(0);
}
while((ch=getchar())!='
')
fputc( 【2】 ,fp);
【3】 ;
}
举一反三
- 有以下程序 #include #include main() { FILE*fp; charch,str[5]; if((fp=fopen('file.txt','wb+'))==NULL) { printf('error! '); exit(0); } ch=getchar(); while(ch!='!') { fputc(ch,fp); ch=getchar(); } rewind(fp); fgets(str,4,fp); while(!feof(fp)) { puts(str); fgets(str,4,fp); } fclose(fp); } 程序运行时输入:c:est!,则程序的输出结果是
- 以下程序中用户由键盘输入一个文件名,然后输入一串字符(用#结束输入)存放到此文件文件中形成文本文件并将字符的个数写到文件尾部,请填空。#include"stdio.h"#include"stdlib.h"voidmain(){FILE*fp;charch,fname[32];intcount=0;printf("Inputthefilename:");scanf("%s",fname);if((fp=fopen())==NULL){printf("Can'topenfile:%s ",fname);exit(0);}getchar();printf("Enterdata:");while((ch=getchar())!='#'){fputc(ch,fp);count++;}fprintf(," %d ",count);fclose(fp);}
- 下面程序把从终端读入的文本(用@作为文本结束标志)输出到一个名为bi.dat的新文件中。请填空。 #include "stdio.h" FILE *fp; { char ch; if( (fp=fopen ( 【 】 ) )= = NULL)exit(0); while( (ch=getchar( )) !='@') fputc (ch,fp); fclose(fp); }
- 下面程序的输出结果是________。#include<;stdio.h>;int main( ){ FILE *fp; int n,a[2]={65,66}; char ch; fp=fopen("d.dat","w"); fprintf(fp,"%d%d",a[0],a[1]); fclose(fp); fp=fopen("d.dat","r"); fscanf(fp,"%c",&ch); n=ch; while(n!=0){printf("%d",n%10);n=n/10;} fclose(fp); return 0;}
- 执行以下程序后,test.txt文件的内容是(若文件能正常打开) ( )。#include <stdio.h>#include <stdlib.h>main( ) FILE * fp; char * s1 = "Fortran" , * s2 = "Basic"; if((fp = fopen( "test. txt" ," wb" )) = = NULL) prinff( "Can’t open test. txt file \n"); exit(1); fwrite( s1 ,7,1 ,fp); /* 把从地址s1开始到7个字符写到fp所指文件中*/ fseek(fp,OL,SEEK_SET); /*文件位置指针移到文件开头*/ fwrite (s2,5,1,fp); felose (fp); A: Basiean B: BasieFortran C: Basic D: FortranBasie