程序设计题
读入文件d:\\example\\c1.txt ,在屏幕上输出。
#include<stdio.h> #define N int main(){ FILE *fp; char ch; fp = fopen("d:\\example\\c1.txt",rb) if(fp == NULL){ printf("打开文件失败:"); return -1; } ch =fgetc(fp); while(ch != EOF){ putchar(ch); ch = fgetc(fp); } fclose(fp); return 0; }
#include #include int main() { FILE* p; p=fopen("d:\\example\\c1.txt","r"); if(p==NULL) { printf("NO!"); exit(0); } char ch=fgetc(p); while(ch!=EOF) { putchar(ch); ch=fgetc(p); } return 1; }
#include <stdio.h> #include <stdlib.h> int main() { FILE* p; p=fopen("d:\\example\\c1.txt","r"); if(p==NULL) { printf("NO!"); exit(0); } char ch=fgetc(p); while(ch!=EOF) { putchar(ch); ch=fgetc(p); } return 1;
}
#include<stdio.h>
int main(){
FILE *fp;
fp=fopen("d:\\example\\c1.txt",'r');
while ((ch = fgetc(file)) != EOF)
{ putchar(ch); }
fclose(file);
return 0;
#include main(){ FILE* fp = fopen(“d:/example/c1.txt”, “r+”); if(fp==NULL) exit(0); char ptr; while(!feof(fp)){ ptr = fgetc(fp); printf(“%c”, ptr); } fclose(fp); }
#include #include int main(){ FILE *fp; if((fp=fopen("d:\\example\\c1.txt","r"))==NULL){ printf("error"); exit(0); } char ch=fgetc(fp); while(ch!=EOF){ putchar(ch); ch=fgetc(fp); } fclose(fp); }
#include<stdlib.h>
int main()
{
FILE *p;
p=fopen("d:\\example\\c1.txt","r")
char ch = fgetc(p);
while(ch!=EOF)
putchar(ch);
ch=fgetc(p);
#include <stdio.h> #include <stdlib.h> int main() { FILE* p; p=fopen("d:\\example\\c1.txt","r"); if(p==NULL) { printf("NO!"); exit(0); } char ch=fgetc(p); while(ch!=EOF) { putchar(ch); ch=fgetc(p); } return 1; }
#include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("d:\\example\\c1.txt", "r"); while(1) { ch = fgetc(fp); putchar(ch); if(feof(fp)) { break; } } fclose(fp); return 0; }
#include<stdio.h> #include<stdlib.h> int main() { FILE *fp = fopen("D:\\example\\c1.txt","r"); if(fp == NULL) { printf("打开文件错误,程序退出"); exit(0); } char ch; while((ch = fgetc(fp)) != EOF) { fputc(ch, stdout); } fclose(fp); return 0; }
#include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; char filename[20]="D://test.txt"; if((fp=fopen(filename,"w"))==NULL){ printf("没有这个文件"); exit(0); } char ch; printf("请输入字符串以#结尾:\n"); ch=getchar(); while(ch!='#'){ putchar(ch); fputc(ch,fp); ch=getchar(); } fclose(fp); putchar(10); return 0; }
zmh 回复 zmh: #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; char filename[20]="d:\\example\\c1.txt"; if((fp=fopen(filename,"r"))==NULL){ printf("没有这个文件"); exit(0); } char ch; while(ch!=EOF){ ch=fgetc(fp); putchar(ch); } fclose(fp); }
#include #include "stdio.h" int main() { FILE *fp; if ((fp = fopen("d:/example/c1.txt", "rt")) == NULL) { exit(1); } char ch = fgetc(fp); while (ch!=EOF){ putchar(ch); ch= fgetc(fp); } fclose(fp); return 0; }
#include <stdlib.h> #include "stdio.h" int main() { FILE *fp; if ((fp = fopen("d:/example/c1.txt", "rt")) == NULL) { exit(1); } char ch = fgetc(fp); while (ch!=EOF){ putchar(ch); ch= fgetc(fp); } fclose(fp); return 0; }
#include<stdio.h> int main(){ FILE *p; if((p = fopen("d:\\example\\c1.txt","r")) == NULL){ printf("Open error!\n"); exit(0); } char ch = fgetc(p); while(!feof(p)){ putchar(ch); ch = fgetc(p); } putchar('\n'); fclose(p); return 0; }
#include<stdio...
用户登录可进行刷题及查看答案
main()
char ch;
if((fp=fopen("d:\\example\\c1.txt","rt"))==NULL)
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
ch=fgetc(fp);
fclose(fp);
【解析】本例程序的功能是从文件中逐个读取字符,在屏幕上显示。程序定义了文件指针fp,以读文本文件方式打开文件“d:\\example\\c1.txt”,并使fp指向该文件。如打开文件出错,给出提示并退出程序。程序第12行先读出一个字符,然后进入循环,只要读出的字符不是文件结束标志(每个文件末有一结束标志EOF)就把该字符显示在屏幕上,再读入下一字符。每读一次,文件内部的位置指针向后移动一个字符,文件结束时,该指针指向EOF。执行本程序将显示整个文件。
登录后提交答案