gets与scanf如何判断读取文件结束

gets与scanf如何判断读取文件结束

(1) void * =====NULL


(2)int =======EOF=======0

当出错或到文件末尾时:

scanf() 返回int,故与EOF匹配
gets() 返回char * ,故与NULL匹配

两个例子:
(1) 
#include<stdio.h>
void main()
{
char a[20];
int i=0;

freopen("input.txt","r",stdin);
       while(scanf("%s",a)!=EOF) printf("%s",a);
    
}

#include<stdio.h>
void main()
{
char a[20];
int i=0;

freopen("input.txt","r",stdin);
        while(gets(a)!=NULL) puts(a);
    
}

补充:(1)关于' ' ,'\0' ,NULL ,EOF,空格(ascii码为32) 的区别 :

   ' '和空格 等价

   '\0' 是ascii码为0的字符,即为空字符

     至于 NULL和EOF要依情况而定了。   

(2)

scanf()函数返回成功赋值的数据项数,出错时则返回EOF


gets()用于从标准输入流stdin读入一个整行(以'\n'或EOF)结束,写入ptr指向的字符数组,并返回这

个指针;出错或遇到文件结束时则返回NULL。行末的'\n'从流中取出,但不写入数组。gets()不检查被写

入的数组大小。

getchar()用于从标准输入流stdin读入一个字符,并返回这个字符。如果读到文件结尾,则返回EOF。注

意到EOF不能用char类型表示,所以getchar()函数返回的是一个int型的数。使用时也应该注意这一点。

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM的,你懂得 ACM做题过程中的一些小技巧。 1.一般用C语言节约空间,要用C++库函数或STL时才用C++; cout、cin和printf、scanf最好不要混用。 大数据输入输出时最好不要用cin、cout,防止超时。 2.有时候int型不够用,可以用long long或__int64型(两个下划线__)。 值类型表示值介于 -2^63 ( -9,223,372,036,854,775,808) 到2^63-1(+9,223,372,036,854,775,807 )之间的整数。 printf("%I64d",a); //__int64 一般VC编译器使用(虽然有的OJ用g++,但是动态链接库用的windows的,所以要用%I64d输入输出) printf("%lld",a); //long long 一般g++编译器使用 3.OJ判断是只看输出结果的,所以不要要多余的提示输出。 所以大部分题处理一组数据后可以直接输出,就不需要用数组保存每一个Case的数据。 while(case--) { scanf(...); ...... printf(...); } 4.纯字符串用puts()输出。 数据大时最好用scanf()、printf()减少时间。 先用scanf(),再用gets()会读入回车。所以在中间加一个getchar(); scanf("%c%c",&c1,&c2)会读入空格;建议用%s读取字符串,取第一个字符。 5. 读到文件的结尾,程序自动结束 while( ( scanf(“%d”, &a) ) != -1 ) while( ( scanf(“%d”, &a) ) != EOF) while( ( scanf(“%d”, &a) ) == 1 ) while( ~( scanf(“%d”, &a) ) ) 读到一个0时,程序结束 while( scanf(“%d”, &a) , a) while( scanf(“%d”, &a)!=EOF && a) 读到多个0时,程序结束 while( scanf(“%d%d%d”, &a, &b, &c), a+b+c ) //a,b,c非负 while( scanf(“%d%d%d”, &a, &b, &c), a|b|c ) 6.数组定义int a[10] = {0};可以对其全部元素赋值为0; 数组太大不要这样,防止CE。 全局变量,静态变量自动初始化为0; 函数中定义的变量存储在栈空间中,数组太大需要定义为全局变量(存储在堆空间中)。 7.有很多数学题是有规律的,直接推公式或用递归、循环。 8.圆周率=acos(-1.0) 自然对数=exp(1.0) 9.如果要乘或除2^n,用位移运算速度快。a>>n;a<b?a:b; } int gcd(int m,int n) { return n?gcd(n,m%n):m; } int abs(int a) { return an; } sort(a,a+n,cmp); 14.有的题数据范围小但是计算量大可以用打表法 先把结果算出来保存在数组里,要用时直接取出来。 15.浮点数比较时最好控制精度 #define eps 1e-6 fabs(a-b)<eps 16.有些字符串与整型的转换函数是非标准的 可以使用sscanf()和sprintf()代替 sscanf(s,"%d",&n);//从字符串s中读入整数n sprintf(s,"%d",n);//将n转换为字符串s
////////预处理头文件////// #include<iostream.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<conio.h> #define N 20 //所输入页的行数 #define size_l 80 //每行的字符数 typedef struct Line{ char *data; //数据域 struct Line *next; //指针域 }Line; Line *head=NULL; //定义一个全局变量指针,头结点 void InitWord(Line* &head) { printf("请输入一页文字(以Ctrl+E表示结束,每行最多输入80个字符!):\n"); Line* p=new Line; //定义一个Line*指针 head=p; char buffer[100]; while(1) { gets(buffer); if(strlen(buffer)>80) { printf("每行最多输入80字符\n"); break; } if(buffer[0]==5) break; //如果发现^E,退出 p->data=new char[strlen(buffer)+1]; strcpy(p->data,buffer); if(buffer[strlen(buffer)-1]==5) { p->data[strlen(buffer)-1]='\0'; break; } p->next=new Line; p=p->next; } p->next=NULL; } ////////统计字母数//////// int CountLetter(Line* &head) { Line *p=head; int CountL=0; while(p!=NULL) { int len=strlen(p->data); //计算当前行的长度 for(int i=0;i<len;i++) if((p->data[i]>='a'&&p->data[i]<='z')||(p->data[i]>='A'&&p->data[i]<='Z')) //判断字符的条件 CountL++; p=p->next; } return CountL; } ///////统计数字数量//////////// int CountDigit(Line* &head) { Line *p=head; int CountD=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]>=48&&p->data[i]<=57) CountD++; p=p->next; } return CountD; } //////统计空格数///////// int CountSpace(Line* &head) { Line *p=head; int CountS=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]==32) CountS++; p=p->next; } return CountS; } ///////统计文章总字数//////////// int CountWord(Line* &head) { Line *p=head; int CountW=0; while(p!=NULL) { CountW+=strlen(p->data); p=p->next; } return CountW; } ////////统计文字数///////// int CountHanzi(Line* &head) { Line *p=head; int CountH=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]&0x80) { CountH++; } p=p->next; } return int(CountH/2); } ////////统计测试字符串在文章中出现的次数/////////// int CountString(Line* &head,char* S) { Line *p=head; int count=0; int i,j,pos; int len=strlen(S); while(p!=NULL) { int len1=strlen(p->data); i=0; j=0; pos=i; G1: while(i<len1&&j<len) { if(p->data[i]==S[j]) { i++; j++; } else { i=i-j+1; j=0; } } if(j>=len) { count++; j=0; goto G1; } p=p->next; } return count; } ///////删除指定的字符串///////// void Delete(char* str,char *S) //把字符串S从Str中删除 { char *p=strstr(str,S); while(p!=NULL) { char buffer[80]; int len=strlen(str); int i=len-strlen(p); int j=i+strlen(S); int count=0; for(int m=0;m<i;m++) buffer[count++]=str[m]; for(int n=j;n<len;n++) buffer[count++]=str[n]; buffer[count]='\0'; strcpy(str,buffer); p=strstr(str,S); } } //////////删除文章中某一字符串//////////// void DeleteString(Line* &head,char* S) { Line *p=head; Line *q=p; while(p!=NULL) { if(strstr(p->data,S)!=NULL) //能够找到指定字符串 { if(strlen(p->data)==strlen(S)) { Delete(p->data,S); q->next=p->next; delete p; p=q; } else { Delete(p->data,S); q=p; } } p=p->next; } } ////////输出文章///////// void PrintWord(Line * &head) { Line *p=head; while(p!=NULL) { printf("%s\n",p->data); p=p->next; } } void main() { Line *head=NULL; int a; char c; char s1[80]; char s2[80]; G2: printf("*****************文章编辑********************\n"); printf("---------------------------------------------\n"); printf("******1.新建一页文字 2.统计 *******\n"); printf("******3.删除字符串 4.输出文章 *******\n"); printf("******5.刷新 6.退出 *******\n"); printf("---------------------------------------------\n"); LOOP: printf("请选择(输入主菜单中选项前的数字):"); scanf("%d",&a); switch(a) { case 1: InitWord(head); printf("--------输出文章:-----"); PrintWord(head); goto LOOP; case 2: printf("***A.统计字符串 B.统计字符数量***\n"); printf("请选择(输入A或B):"); cin>>c; if(c=='A') { printf("请输入要统计的字符串:"); scanf("%s",s1); printf("该指定的字符串出现的次数为:%d 次\n",CountString(head,s1)); } else if(c=='B') { printf("文章全部字母数:%d\n",CountLetter(head)); printf(" 全部数字个数:%d\n",CountDigit(head)); printf(" 全部空格个数:%d\n",CountSpace(head)); printf(" 全部汉字个数:%d\n",CountHanzi(head)); printf(" 总字数:%d\n",CountWord(head)); } else printf("没有这个选项!!!\n"); goto LOOP; case 3: printf("------请输入要删除的字符串:"); scanf("%s",s2); DeleteString(head,s2); printf("删除后的文章为:"); PrintWord(head); goto LOOP; case 4: if(head==NULL) { printf("文章没有任何内容!!!\n"); goto LOOP; } printf("-------输出的文章为:------"); PrintWord(head); goto LOOP; case 5: system("cls"); goto G2; case 6: printf("退出程序!\n"); return ; default: printf("!!没有该选项,是否想退出程序?(Y/N):"); cin>>c; if(c=='Y') goto LOOP; else return ; } return ; } ////////预处理头文件////// #include<iostream.h> #include<string.h> #include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<conio.h> #define N 20 //所输入页的行数 #define size_l 80 //每行的字符数 typedef struct Line{ char *data; //数据域 struct Line *next; //指针域 }Line; Line *head=NULL; //定义一个全局变量指针,头结点 void InitWord(Line* &head) { printf("请输入一页文字(以Ctrl+E表示结束,每行最多输入80个字符!):\n"); Line* p=new Line; //定义一个Line*指针 head=p; char buffer[100]; while(1) { gets(buffer); if(strlen(buffer)>80) { printf("每行最多输入80字符\n"); break; } if(buffer[0]==5) break; //如果发现^E,退出 p->data=new char[strlen(buffer)+1]; strcpy(p->data,buffer); if(buffer[strlen(buffer)-1]==5) { p->data[strlen(buffer)-1]='\0'; break; } p->next=new Line; p=p->next; } p->next=NULL; } ////////统计字母数//////// int CountLetter(Line* &head) { Line *p=head; int CountL=0; while(p!=NULL) { int len=strlen(p->data); //计算当前行的长度 for(int i=0;i<len;i++) if((p->data[i]>='a'&&p->data[i]<='z')||(p->data[i]>='A'&&p->data[i]<='Z')) //判断字符的条件 CountL++; p=p->next; } return CountL; } ///////统计数字数量//////////// int CountDigit(Line* &head) { Line *p=head; int CountD=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]>=48&&p->data[i]<=57) CountD++; p=p->next; } return CountD; } //////统计空格数///////// int CountSpace(Line* &head) { Line *p=head; int CountS=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]==32) CountS++; p=p->next; } return CountS; } ///////统计文章总字数//////////// int CountWord(Line* &head) { Line *p=head; int CountW=0; while(p!=NULL) { CountW+=strlen(p->data); p=p->next; } return CountW; } ////////统计文字数///////// int CountHanzi(Line* &head) { Line *p=head; int CountH=0; while(p!=NULL) { int len=strlen(p->data); for(int i=0;i<len;i++) if(p->data[i]&0x80) { CountH++; } p=p->next; } return int(CountH/2); } ////////统计测试字符串在文章中出现的次数/////////// int CountString(Line* &head,char* S) { Line *p=head; int count=0; int i,j,pos; int len=strlen(S); while(p!=NULL) { int len1=strlen(p->data); i=0; j=0; pos=i; G1: while(i<len1&&j<len) { if(p->data[i]==S[j]) { i++; j++; } else { i=i-j+1; j=0; } } if(j>=len) { count++; j=0; goto G1; } p=p->next; } return count; } ///////删除指定的字符串///////// void Delete(char* str,char *S) //把字符串S从Str中删除 { char *p=strstr(str,S); while(p!=NULL) { char buffer[80]; int len=strlen(str); int i=len-strlen(p); int j=i+strlen(S); int count=0; for(int m=0;m<i;m++) buffer[count++]=str[m]; for(int n=j;n<len;n++) buffer[count++]=str[n]; buffer[count]='\0'; strcpy(str,buffer); p=strstr(str,S); } } //////////删除文章中某一字符串//////////// void DeleteString(Line* &head,char* S) { Line *p=head; Line *q=p; while(p!=NULL) { if(strstr(p->data,S)!=NULL) //能够找到指定字符串 { if(strlen(p->data)==strlen(S)) { Delete(p->data,S); q->next=p->next; delete p; p=q; } else { Delete(p->data,S); q=p; } } p=p->next; } } ////////输出文章///////// void PrintWord(Line * &head) { Line *p=head; while(p!=NULL) { printf("%s\n",p->data); p=p->next; } } void main() { Line *head=NULL; int a; char c; char s1[80]; char s2[80]; G2: printf("*****************文章编辑********************\n"); printf("---------------------------------------------\n"); printf("******1.新建一页文字 2.统计 *******\n"); printf("******3.删除字符串 4.输出文章 *******\n"); printf("******5.刷新 6.退出 *******\n"); printf("---------------------------------------------\n"); LOOP: printf("请选择(输入主菜单中选项前的数字):"); scanf("%d",&a); switch(a) { case 1: InitWord(head); printf("--------输出文章:-----"); PrintWord(head); goto LOOP; case 2: printf("***A.统计字符串 B.统计字符数量***\n"); printf("请选择(输入A或B):"); cin>>c; if(c=='A') { printf("请输入要统计的字符串:"); scanf("%s",s1); printf("该指定的字符串出现的次数为:%d 次\n",CountString(head,s1)); } else if(c=='B') { printf("文章全部字母数:%d\n",CountLetter(head)); printf(" 全部数字个数:%d\n",CountDigit(head)); printf(" 全部空格个数:%d\n",CountSpace(head)); printf(" 全部汉字个数:%d\n",CountHanzi(head)); printf(" 总字数:%d\n",CountWord(head)); } else printf("没有这个选项!!!\n"); goto LOOP; case 3: printf("------请输入要删除的字符串:"); scanf("%s",s2); DeleteString(head,s2); printf("删除后的文章为:"); PrintWord(head); goto LOOP; case 4: if(head==NULL) { printf("文章没有任何内容!!!\n"); goto LOOP; } printf("-------输出的文章为:------"); PrintWord(head); goto LOOP; case 5: system("cls"); goto G2; case 6: printf("退出程序!\n"); return ; default: printf("!!没有该选项,是否想退出程序?(Y/N):"); cin>>c; if(c=='Y') goto LOOP; else return ; } return ; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值