首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于资料EOF标志的添加

2012-09-04 
关于文件EOF标志的添加?#includestdio.hmain(){int accountchar name[30]double balanceFILE *cfPtr

关于文件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不過是文件類函數讀到結尾時返回的一個結束標志

热点排行