LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 727|回复: 3

兄弟们解释下为什么文件长度为0

[复制链接]
发表于 2004-11-15 21:26:58 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>

  6. int main(void)
  7. {
  8.         int fd, i, ch;
  9.         FILE *fp;

  10.         fd = open("abc", O_RDWR | O_CREAT, 00644);
  11.         lseek(fd, 50, SEEK_SET);
  12.         fp = fdopen(fd, "r+");
  13.        
  14.         for(i='a'; i<='z'; i++){
  15.                 ungetc(i, fp);
  16.         }

  17.         for(i='A'; i<='Z'; i++){
  18.                 ch = fgetc(fp);
  19.                 putchar(ch);
  20.         }
  21.         putchar('\n');
  22.         fclose(fp);

  23.         exit(0);
  24. }
复制代码

执行后查看 abc 文件长度为0 为什么呢?:help :help
发表于 2004-11-16 03:28:35 | 显示全部楼层
int  ungetc ( int character , FILE * stream );

Push a character back into stream.
  A character is pushed into an input stream where last character was read and the file pointer is reset to that previous position. This character will be returned by the next call to getc or fread for the same stream.
  If the End-Of-File indicator was set is cleared after a call to this function.
  A call to fflush, fseek, fsetpos or rewind for this stream will undo the effects of any previous call to ungetc.
  This function do not affect the file associated with the stream, which will remain unchanged by any call to this function.

http://www.cplusplus.com/ref/cstdio/ungetc.html
 楼主| 发表于 2004-11-16 08:56:16 | 显示全部楼层

多谢:) 原来不会影响文件

原来不会影响文件:p
发表于 2004-11-16 15:58:49 | 显示全部楼层
UP
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表