读书人

关于资料EOF标志的添加

发布时间: 2012-09-04 14:19:30 作者: rapoo

关于文件EOF标志的添加?
#include<stdio.h>

main()
{
int account;
char name[30];
double balance;

FILE *cfPtr;

if((cfPtr = fopen("client.dat", "w")) == NULL)
printf("The file could not be opened");
else{
printf("Please enter the account, name, and balance.\n");
printf("Enter EOF to end.\n");
printf("? ");
scanf("%d%s%lf", &account, name, &balance);
while(!feof(stdin)){
fprintf(cfPtr, "%d %s %.2lf ", account, name, balance);
printf("? ");
scanf("%d%s%lf", &account, name, &balance);

}
}
fclose(cfPtr); /* 结束输入,程序是否自动将EOF标志添加到cfPtr指针对应的文件中? */
}

[解决办法]
我也曾也主同疑惑
不在明白EOF的含了

我先一起看看FILE是怎么定的:
FILE <STDIO.H>

File control structure for streams.

typedef struct {
short level;
unsigned flags; char fd;
unsigned char hold;
short bsize;
unsigned char *buffer, *curp;
unsigned istemp;
short token;
} FILE;

再看看flags是怎么定的:
_F_xxxx <STDIO.H>

File status flags of streams

Name ?Meaning
_F_RDWR ?Read and write
_F_READ ?Read-only file
_F_WRIT ?Write-only file
_F_BUF ?Malloc'ed buffer data
_F_LBUF ?Line-buffered file
_F_ERR ?Error indicator
_F_EOF ?EOF indicator
_F_BIN ?Binary file indicator
_F_IN ?Data is incoming
_F_OUT ?Data is outgoing
_F_TERM ?File is a terminal
}

在看看EOF在文件中是怎么定的:
/*EOF a constant indicating that the end-of-file has been reached on a file*/

#define _F_EOF 0x0020 /* EOF indicator */
#define EOF (-1) /* End of file indicator */
EOF在fread等文件函的返回值做比,替(-1)的
在文件中根本不存在EOF西,EOF不是文件函到尾返回的一束志

读书人网 >C语言

热点推荐