C是一个锋利的工具 如果您正确阅读标准,您会看到如果fgets在 字符串中遇到eof,它将返回到目前为止读取的字符串。如果遇到 文件结束或错误,它只返回EOF。 - Mark McIntyre CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html> CLC自述文件:< http://www.ungerhu.com/ jxh / clc.welcome.txt> ---- ==通过Newsfeeds.Com发布 - 无限制 - 未经审查 - 安全使用网新闻== ---- http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组 ---- =东海岸和西海岸服务器农场 - 通过加密实现全面隐私= ---- --Mac Hello,I have found some peculiar behaviour in the fgets runtime libraryfunction for my compiler/OS/platform (Dev C++/XP/P4) - making a Cconsole program (which runs in a CMD.exe shell).The standard says about fgets:synopsis#include <stdio.h>char *fgets(char *s, int n, FILE *stream);description"The fgets function returns s if successful. If end-of-file isencountered and no characters have been read into the array, thecontents of the array remain unchanged and a null pointer is returned.If a read error occurs during the operation, the array contents areindeterminate and a null pointer is returned."The problem I''m having is, if EOF is provided after another character,fgets simply doesn''t return NULL to flag an error, so my error catchingcode is not finding anything. Also, the EOF in the line, for me, allit does it causes fgets to ignore the EOF -AND- also ignore everythingafter the EOF upto and including the first seen newline.So fgets hangs if I provide it with an EOF, because it basicallydiscards the EOF condition and all characters after it including thenext newline(which normally tells fgets to stop, but in this case itjust discards and ignores it). So I find myself having to hit newlineagain to make fgets -unblock- or wahtever you want to call it.For example, I ran this test code:#include <stdio.h>#include <stdlib.h>#define MAXINPUT 30intmain(void){char sample[MAXINPUT];if(!fgets(sample, MAXINPUT, stdin)){fprintf(stderr, "\nfgets() error, program quitting\n");exit(1);}printf("sample = %s\n", sample);return 0;}My input was:-------------hello[EOF]idiot[\n][space]world[\n]My output was (not including echoes):-------------------------------------sample = hello world[\n][\n]- notice that there are two newlines in the output because fgets storesa newline and my printf format string also has oneAs you can see, all fgets did was ignore everything from the EOF uptoand including the next newline. It definitely ignored this newline,because fgets stayed -blocked- and the program was still waiting for myinput (as if it required another newline) - so I typed a space and''world'' then hit enter (this enter/newline registered properly andfgets unblocks) and fgets assumed it go the input {hello world\n} andjust ignored/discarded the middle part: {[EOF]idiot\n} - ignore thebraces, they are used as delimitters only.This wouldn''t bother me if fgets returned a null pointer like thestandard said it would, because I would really like to trap this typeof stupid user input and deal with it, but I can''t even do that.Can someone test this program on their system or tell me what I''mmissing? Isn''t fgets supposed to return a null pointer?thanking everyone very much,Tinesan Troy, B.Eng (mech) ti*****@gmail.com 解决方案How did you provide it from the keyboard ?--EmmanuelThe C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.htmlThe C-library: http://www.dinkumware.com/refxc.html"C is a sharp tool"If you read the standard correctly, you''ll see that if fgets encounters eof in astring, it returns the string read so far. It only returns EOF if it encountersend-of-file or an error.--Mark McIntyreCLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups----= East and West-Coast Server Farms - Total Privacy via Encryption =------Mac 这篇关于fgets,EOF在行中间,不会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 18:52