本文介绍了while循环读取文件时sizeof的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我写了一个小的测试程序来读取数据文件并打印每个 行,但是它只打印了3行中的第2行。 测试文件foo.txt有3行: 7388:Zn-> Z0运行懦夫! 8473:Q1-> P2哈哈哈哈! 8381:G3->每个人都好确定好! 但是当我运行我的程序时它只打印第二行: 即将打开文件进行阅读 测试如果fp流被打开 进入循环时 即将拨打fgets 即将拨打printf 8473:Q1-> P2哈哈哈哈! 即将打电话给fgets 即将打电话给printf 关闭文件 即将退出 另外这个警告意味着什么,我怎么能消除它: foo.c:在函数''main'': foo.c:33:警告:不兼容隐含声明内置 功能'退出'' 这是我的代码: #include< stdio.h> int main(无效) { FILE * fp; char s [80]; printf(打开文件阅读\ n); fp = fopen(" foo.txt"," r"); printf(测试fp流是否打开了\ n); if(fp!= NULL) { printf(输入while loop\\\); while((fgets(s,sizeof(s),fp)) != NULL) { printf(即将调用fgets \ n); fgets(s,sizeof(s ),fp); printf(即将调用printf \ n); printf("%s \ n",s); } } printf(" closing file \ n"); fclose(fp); printf(" about to exit\\\); exit(0); } Lisp 9000 解决方案 文章< 11 ********** ************@r29g2000hsg.googlegroups .com> ;, li ***** *@gmail.com < li ****** @ gmail.comwrote: while((fgets(s,sizeof(s) ,fp))!= NULL) 这里你打电话给fgets,不打印结果。 { printf(即将调用fgets \ n); fgets(s,sizeof(s),fp); 在这里你再打电话。 - Richard - 考虑在一些字母表中需要多达32个字符 - 1963年的X3.4。 9月14日晚上7:41,富... @ cogsci.ed.ac.uk(Richard Tobin)写道: 文章< 1189813623.280906.180 ... @ r29g2000hsg.googlegroups .com>, lisp9 ... @ gmail.com< lisp9 ... @ gmail.comwrote: while((fgets(s,sizeof(s),fp))!= NULL) 在这里你打电话给fgets,不打印结果。 { printf(即将调用fgets \ n); fgets(s,sizeof(s),fp); 在这里你再打电话给它。 哎呀。谢谢理查德。这个警告意味着什么,我怎么能消除它: foo.c:在函数''main'': foo .c:33:警告:内置不兼容的隐式声明 函数''退出'' Lisp 9000 "李****** @ gmail.com"写道: > 我写了一个小测试程序来读取数据文件并打印每个 行,但它只打印3行中的第2行。 测试文件foo.txt有3行: 7388:Zn-> Z0跑懦夫! 8473:Q1-> P2哈哈哈哈! 8381:G3-> EVERYONE好的确定好吧! 但是当我运行我的程序时它只打印第二行: .... snip ... > 这是我的代码: #include< stdio.h中> 其中#include< stdlib.h(退出函数) > int main(无效){ FILE * fp; char s [80]; printf(即将打开文件以便阅读\ n); fp = fopen(" foo.txt"," r"); printf (测试fp流是否已打开\ n); if(fp!= NULL){ printf(" while while loop\\\) (;(fgets(s,sizeof(s),fp))!= NULL){ printf(" about to call fgets\\\) ); 您刚刚调用了fgets。消除这一行。 fgets(s,sizeof(s),fp); 这一行。它只是丢弃刚读过的行。 printf(" about to call printf\\\); printf(" %s \ n",s); } } printf(" closing file \\\"); fclose(fp); printf(" about to exit\\\); exit(0); 您可以使用返回0这里取代退出。 } - Chuck F(cinefalconer at maineline dot net) 可用于咨询/临时嵌入式和系统。 < http://cbfalconer.home.att.net> - 通过 http://www.teranews.com I wrote a small test program to read a file of data and print eachline, but it''s only printing the 2nd line out of 3 total lines.The test file, "foo.txt", has 3 lines:7388: Zn->Z0 Run coward!8473: Q1->P2 HAHAHAHAHA!8381: G3->EVERYONE ok ok ok!But when I run my progam it only prints the second line:about to open file for readingtesting if fp stream was openedentering while loopabout to call fgetsabout to call printf8473: Q1->P2 HAHAHAHAHA!about to call fgetsabout to call printfclosing fileabout to exitAlso what does this warning mean and how can I eliminate it:foo.c: In function ''main'':foo.c:33: warning: incompatible implicit declaration of built-infunction ''exit''Here is my code:#include <stdio.h>int main(void){FILE *fp;char s[80];printf("about to open file for reading\n");fp = fopen("foo.txt","r");printf("testing if fp stream was opened\n");if (fp != NULL){printf("entering while loop\n");while ( (fgets(s, sizeof(s), fp)) != NULL){printf("about to call fgets\n");fgets(s,sizeof(s),fp);printf("about to call printf\n");printf("%s\n",s);}}printf("closing file\n");fclose(fp);printf("about to exit\n");exit(0);}Lisp 9000 解决方案 In article <11**********************@r29g2000hsg.googlegroups .com>, li******@gmail.com <li******@gmail.comwrote: while ( (fgets(s, sizeof(s), fp)) != NULL)Here you call fgets, and don''t print the result. { printf("about to call fgets\n"); fgets(s,sizeof(s),fp);Here you call it again.-- Richard--"Consideration shall be given to the need for as many as 32 charactersin some alphabets" - X3.4, 1963.On Sep 14, 7:41 pm, [email protected] (Richard Tobin) wrote:In article <[email protected] .com>,[email protected] <[email protected]: while ( (fgets(s, sizeof(s), fp)) != NULL)Here you call fgets, and don''t print the result. { printf("about to call fgets\n"); fgets(s,sizeof(s),fp);Here you call it again.Oops. Thanks Richard. And what does this warning mean and how can Ieliminate it:foo.c: In function ''main'':foo.c:33: warning: incompatible implicit declaration of built-infunction ''exit''Lisp 9000 "li******@gmail.com" wrote:>I wrote a small test program to read a file of data and print eachline, but it''s only printing the 2nd line out of 3 total lines.The test file, "foo.txt", has 3 lines:7388: Zn->Z0 Run coward!8473: Q1->P2 HAHAHAHAHA!8381: G3->EVERYONE ok ok ok!But when I run my progam it only prints the second line:.... snip ...>Here is my code:#include <stdio.h>Where is #include <stdlib.h(for exit function)>int main(void) { FILE *fp; char s[80]; printf("about to open file for reading\n"); fp = fopen("foo.txt","r"); printf("testing if fp stream was opened\n"); if (fp != NULL) { printf("entering while loop\n"); while ( (fgets(s, sizeof(s), fp)) != NULL) { printf("about to call fgets\n");You just called fgets. eliminate this line. fgets(s,sizeof(s),fp);And this line. It just discards the line just read. printf("about to call printf\n"); printf("%s\n",s); } } printf("closing file\n"); fclose(fp); printf("about to exit\n"); exit(0);You could use "return 0" here in place of exit.}--Chuck F (cbfalconer at maineline dot net)Available for consulting/temporary embedded and systems.<http://cbfalconer.home.att.net>--Posted via a free Usenet account from http://www.teranews.com 这篇关于while循环读取文件时sizeof的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 17:03