⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_35335.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:35335 发表者:铁骑柔肠 发表日期:2003-04-05 20:42:50
<br>主题:关于fseek
<br>内容:#include"stdio.h"<BR>#include"ctype.h"<BR><BR>int main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE *fp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((fp=fopen("rrr","r+b"))==NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Can't open %s!\n","rrr");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>a=fgetc(fp);<BR>while(a!=EOF)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp; if(islower(a))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a=toupper(a);<BR>&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp,-1,1);<BR>&nbsp;&nbsp;&nbsp;&nbsp; printf("%c",a);<BR>&nbsp;&nbsp;&nbsp;&nbsp; fputc(a,fp);<BR>&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp,0,1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* 为什么一定要这句呢??(去掉了就死循环了)*/<BR>&nbsp;&nbsp;&nbsp;&nbsp; a=fgetc(fp); /* if placed in the first line? */<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:悌悌 回复日期:2003-04-07 14:00:57
<br>内容:铁骑柔肠:<BR> 知道了怎么回事么?我单步跟踪了一下,并用ftell()来跟踪文件指针,我也想不通是怎么回事。我修改后的代码如下:<BR>#include "stdafx.h"<BR>#include "stdlib.h"<BR>#include"stdio.h"<BR>#include"ctype.h"<BR><BR>int main(void)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;long m;<BR>&nbsp;&nbsp;&nbsp;&nbsp;FILE *fp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;char a;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if((fp=fopen("c:\\12.txt","r+b"))==NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Can't open %s!\n","rrr");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;m=ftell(fp);//跟踪文件指针<BR>a=fgetc(fp);<BR>&nbsp;&nbsp;m=ftell(fp);//跟踪文件指针<BR>while(a!=EOF)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp; if(islower(a))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a=toupper(a);<BR>&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp,-1,1);<BR>&nbsp;&nbsp;&nbsp;&nbsp; m=ftell(fp);//跟踪文件指针<BR>&nbsp;&nbsp;&nbsp;&nbsp; printf("%c",a);<BR>&nbsp;&nbsp;&nbsp;&nbsp; fputc(a,fp);<BR>&nbsp;&nbsp;&nbsp;&nbsp; m=ftell(fp);//跟踪文件指针<BR>&nbsp;&nbsp;&nbsp;&nbsp; fseek(fp,0,1);&nbsp;&nbsp;/* 为什么一定要这句呢??(去掉了就死循环了)*/<BR>&nbsp;&nbsp;&nbsp;&nbsp; m=ftell(fp);//跟踪文件指针<BR>&nbsp;&nbsp;&nbsp;&nbsp; a=fgetc(fp); /* if placed in the first line? */<BR>&nbsp;&nbsp;&nbsp;&nbsp; m=ftell(fp);//跟踪文件指针<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>return 0;<BR>} 
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:yingpf 回复日期:2003-04-08 14:59:13
<br>内容:去掉了fseek(fp,0,1); 没有死循环啊,只不过只转换了第一个字符。<BR>为什么移动0 个字符也会有影响呢?真想不通。。。。。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:悌悌 回复日期:2003-04-09 08:54:37
<br>内容:怎么没有人来解决呢?高手请多帮忙呀!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:neilgan 回复日期:2003-04-09 11:57:13
<br>内容:Very interesting question.<BR><BR>The reason is: fputc doesn't really put the char to the file, it just puts it into a buffer, but fgetc still uses the old content, so it returns the second character repeatly.<BR><BR>The solution is: put a fflush(fp) after fputc to force the flush of the stream.<BR><BR>fseek calls fflush internally.<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:悌悌 回复日期:2003-04-11 13:48:12
<br>内容:请问neilgan哪里有相关的文档说明么?能介绍一下么?多谢的了。不过,我还是觉得不是很能让人信服。fputc()是把字符送入到buffer,但在fputc()后我对文件指针的测试即:m=ftell()得到的结果是文件指针指向了下一个字符。按你的说法,ftell()判断的也是buffer里的指针?显然不对的呀。希望得到解答,多谢<BR>2003-4-11 13:57:00

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -