scanf("%c",&ch)!=EOF
时间: 2024-01-02 15:01:48 浏览: 39
scanf("%c",&ch)!=EOF的意思是当输入的字符不等于文件结束标志EOF时,循环会继续执行。在C语言中,EOF的值为-1。所以当输入的字符不是EOF时,循环会一直执行下去。
范例:
```c
#include<stdio.h>
int main() {
char ch;
while(scanf("%c",&ch)!=EOF) {
printf("%c\n",ch+32);
}
return 0;
}
```
这段代码会不断读取输入的字符,并将字符转换为小写字母后输出。循环会一直执行,直到输入的字符为EOF(文件结束标志)为止。
相关问题
#include <stdio.h> #include <stdlib.h> int findHead(FILE * ,int); int findLast(FILE * ,int); void deleteState(FILE **,int * ,int *); void deleteLine(FILE **,int ,int ); int main(){ FILE * fp = fopen("dict.dic","r"); int start,end; if(fp != NULL){ int a,b,c,d,e; fscanf(fp,"%d",&a); fscanf(fp,"%d",&b); fscanf(fp,"%d",&c); fscanf(fp,"%d",&d); fscanf(fp,"%d",&e); int mode; scanf("%d",&mode); switch (mode) { case 1: rewind(fp); start = findHead(fp,5); rewind(fp); end = findLast(fp,a); break; case 2: rewind(fp); start = findHead(fp,a); rewind(fp); end = findLast(fp,b); break; case 3: rewind(fp); start = findHead(fp,b); rewind(fp); end = findLast(fp,c); break; case 4: rewind(fp); start = findHead(fp,c); rewind(fp); end = findLast(fp,d); break; case 5: rewind(fp); start = findHead(fp,d); rewind(fp); end = findLast(fp,e); break; default: printf("??"); break; } rewind(fp); while(ftell(fp)<start) fgetc(fp); while(ftell(fp) != end){ int currentch = fgetc(fp); int flag = 1; if(currentch == '/' ){ int nextch = fgetc(fp); if(nextch == ''){ while(flag && ftell(fp) != end){ currentch = fgetc(fp); while(currentch == '' && ftell(fp) != end){ currentch = fgetc(fp); if(currentch == '/' && ftell(fp) != end) flag = 0; } } } else if(nextch == '/'){ while(currentch != '\n'){ currentch = fgetc(fp); } } else { printf("%c",currentch); currentch = nextch; } } if(ftell(fp) != end && flag) printf("%c",currentch); } } fclose(fp); return 0; } int findHead(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); } int findLast(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); }将子函数并到主函数里
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fp = fopen("dict.dic","r");
int start,end;
if(fp != NULL){
int a,b,c,d,e;
fscanf(fp,"%d",&a);
fscanf(fp,"%d",&b);
fscanf(fp,"%d",&c);
fscanf(fp,"%d",&d);
fscanf(fp,"%d",&e);
int mode;
scanf("%d",&mode);
switch (mode) {
case 1:
rewind(fp);
start = findHead(fp,5);
rewind(fp);
end = findLast(fp,a);
break;
case 2:
rewind(fp);
start = findHead(fp,a);
rewind(fp);
end = findLast(fp,b);
break;
case 3:
rewind(fp);
start = findHead(fp,b);
rewind(fp);
end = findLast(fp,c);
break;
case 4:
rewind(fp);
start = findHead(fp,c);
rewind(fp);
end = findLast(fp,d);
break;
case 5:
rewind(fp);
start = findHead(fp,d);
rewind(fp);
end = findLast(fp,e);
break;
default:
printf("??");
break;
}
rewind(fp);
while(ftell(fp)<start)
fgetc(fp);
while(ftell(fp) != end){
int currentch = fgetc(fp);
int flag = 1;
if(currentch == '/' ){
int nextch = fgetc(fp);
if(nextch == '\n'){
while(flag && ftell(fp) != end){
currentch = fgetc(fp);
while(currentch == ' ' && ftell(fp) != end){
currentch = fgetc(fp);
if(currentch == '/' && ftell(fp) != end)
flag = 0;
}
}
}
else if(nextch == '/'){
while(currentch != '\n'){
currentch = fgetc(fp);
}
}
else {
printf("%c",currentch);
currentch = nextch;
}
}
if(ftell(fp) != end && flag)
printf("%c",currentch);
}
}
fclose(fp);
return 0;
}
int findHead(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
int findLast(FILE * fp,int line){
int ch = 0;
while(line-- && ch != EOF){
ch = fgetc(fp);
while(ch != '\n' && ch != EOF){
ch = fgetc(fp);
}
}
return ftell(fp);
}
#include<stdio.h> void getFiveNum(int a[], FILE *fp); void position(int a[], int num, FILE *fp); int main() { FILE *fp = fopen("dict.dic", "r"); int a[6]; a[0] = 5; getFiveNum(a, fp); int num; scanf("%d", &num); position(a, num, fp); fclose(fp); return 0; } void getFiveNum(int a[], FILE fp) { int ch; for (int i = 1; i <= 5; i++) { ch = fgetc(fp); a[i] = 0; while (ch != 10) { a[i] = a[i] * 10 + (ch - '0'); ch = fgetc(fp); } } } void position(int a[], int num, FILE fp) { int ch, not1 = 0, not2 = 0, line = a[num], enter = 5, chNext; while (enter < a[num - 1]) { ch = fgetc(fp); if (ch == 10) enter++; } ch = fgetc(fp); chNext = fgetc(fp); while (enter < line) { if (not2 == 0 && ch == '/' && chNext == '/') { not1 = 1; }else if (not1 == 0 && ch == '/' && chNext == '') { not2 = 1; }else if (not1 == 1 && chNext == 10) { not1 = 0; ch = chNext; chNext = fgetc(fp); }else if (not2 && ch == '' && chNext == '/') { not2 = 0; ch = chNext; chNext = fgetc(fp); if (chNext == 10) { enter++; } ch = chNext; chNext = fgetc(fp); if (chNext == 10) enter++; } if (not1 == 0 && not2 == 0) { printf("%c", ch); } ch = chNext; chNext = fgetc(fp); if (chNext == 10) { enter++; }else if (chNext == EOF) { if (not1 == 0 && not2 == 0) { printf("%c", ch); break; } else { break; } } } if (not1 == 0 && not2 == 0 && chNext == 10) { printf("%c", ch); } }将子函数并到主函数里
以下是将子函数并到主函数的代码:
#include<stdio.h>
int main() {
FILE *fp = fopen("dict.dic", "r");
int a[6];
a[0] = 5;
int ch, not1 = 0, not2 = 0, line = a[num], enter = 5, chNext;
for (int i = 1; i <= 5; i++) {
ch = fgetc(fp);
a[i] = 0;
while (ch != 10) {
a[i] = a[i] * 10 + (ch - '0');
ch = fgetc(fp);
}
}
int num;
scanf("%d", &num);
while (enter < a[num - 1]) {
ch = fgetc(fp);
if (ch == 10) enter++;
}
ch = fgetc(fp);
chNext = fgetc(fp);
while (enter < line) {
if (not2 == 0 && ch == '/' && chNext == '/') {
not1 = 1;
} else if (not1 == 0 && ch == '/' && chNext == '') {
not2 = 1;
} else if (not1 == 1 && chNext == 10) {
not1 = 0;
ch = chNext;
chNext = fgetc(fp);
} else if (not2 && ch == '' && chNext == '/') {
not2 = 0;
ch = chNext;
chNext = fgetc(fp);
if (chNext == 10) {
enter++;
}
ch = chNext;
chNext = fgetc(fp);
if (chNext == 10) enter++;
}
if (not1 == 0 && not2 == 0) {
printf("%c", ch);
}
ch = chNext;
chNext = fgetc(fp);
if (chNext == 10) {
enter++;
} else if (chNext == EOF) {
if (not1 == 0 && not2 == 0) {
printf("%c", ch);
break;
} else {
break;
}
}
}
if (not1 == 0 && not2 == 0 && chNext == 10) {
printf("%c", ch);
}
fclose(fp);
return 0;
}
注意,将子函数并到主函数的代码,需要将子函数中的参数 FILE *fp 删除,并将 fp 声明为全局变量,否则会编译报错。