使用fgets函数返回值而不是feof

来源:互联网 发布:刘嘉忆 知乎 编辑:程序博客网 时间:2024/04/30 20:42

今天在调试程序的时候,发现cevtor中多了一个数据。函数如下:

隐藏行号 复制代码 原始代码。
  1. void create_testcase::read_districtfiles()
  2. {
  3.     tstring::size_type pos=m_str_logpath.rfind(_T('//'));
  4.     if (tstring::npos==pos) return;
  5.     tstring str_name = m_str_logpath+m_str_logpath.substr(pos);
  6.     FILE *pinfile = _tfopen(str_name.c_str(), _T("rt+, ccs=UTF-8"));
  7.     if (NULL==pinfile) return;
  8.     TCHAR linex[MAX_PATH]=_T("");
  9.     tstring str_line, str_district, str_count;
  10.     while (!feof(pinfile))
  11.     {
  12.         _fgetts(linex, MAX_PATH, pinfile);
  13.         str_line = tstring(linex);
  14.         ...;
  15.         m_name_vector.push_back(tspair(_ttoi(str_count.c_str()), str_district));
  16.     }
  17.     fclose(pinfile);
  18. }
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important} 

加断点看看,最后一个元素重复了,再追,发现原来是文件最后一行被fgets()函数读了两遍。

 
查MSDN,feof,有这么一段话:
“The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been passed. When the end of file is passed, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it.

For example, if a file contains 10 bytes and you read 10 bytes from the file, feof will return 0 because, even though the file pointer is at the end of the file, you have not attempted to read beyond the end. Only after you try to read an 11th byte will feof return a nonzero value.”

注意,上面说的是“the end of stream has been passed, read operations return an end-of-file indicator”,也就是说,只有文件位置指针越过文件结尾以后,read操作才会返回e-o-f的指示符。

 

 

测试1. 创建一个空的txt文件,并使用下面程序测试:

隐藏行号 复制代码 测试代码1。
  1. void TestFun1()
  2. {
  3.     FILE *pinfile = _tfopen(_T("f://12.txt"), _T("rt+"));
  4.     if (NULL==pinfile) return;
  5.     while (!feof(pinfile))
  6.     {
  7.         MessageBox(NULL, _T("test"), _T("debug"), 0);
  8.     }
  9.     fclose(pinfile);
  10. }

结果:啊哦,进入死循环了。
分析:文件位置指针一直没有越过文件结尾,feof一直返回为0,所以进入死循环。

 

 

测试2. 程序更改为:

隐藏行号 复制代码 测试代码2。
  1. void TestFun2()
  2. {
  3.     FILE *pinfile = _tfopen(_T("f://12.txt"), _T("rt+"));
  4.     if (NULL==pinfile) return;
  5.     TCHAR szTest[MAX_PATH]=_T("");
  6.     while (!feof(pinfile))
  7.     {
  8.         _fgetts(szTest, MAX_PATH, pinfile);
  9.         MessageBox(NULL, szTest, _T("debug"), 0);
  10.     }
  11.     fclose(pinfile);
  12. }
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}
结果:弹出一个对话框,内容为空。
分析:fgets函数读MAX_PATH-1个字符以后,文件位置指针已经后移,并且越过了文件结尾,所以下一次调用feof时,返回非0值。

 

 

测试3. 在txt文件中添加数字“1”,TestFun2()函数测试。
结果:只弹出一个对话框,内容为1。
分析:fgets函数读MAX_PATH-1个字符以后,文件位置指针已经后移,并且越过了文件结尾,所以下一次调用feof时,返回非0值。

 

 

测试4. 在txt文件中1后面加上回车符,TestFun2()函数测试。
结果:弹出两个对话框,内容都为1。
分析:fgets第一次,遇到换行符,停止,此时文件位置指针已经到达文件尾部,但是还没有越过,所以,while没有退出, 因为文件位置指针已经到文件尾部,所以第二次fgets没有读到任何数据。

 

 

测试5. 文件内容不变,使用下面程序测试

隐藏行号 复制代码 测试代码2。
  1. void TestFun3()
  2. {
  3.     FILE *pinfile = _tfopen(_T("f://12.txt"), _T("rt+"));
  4.     if (NULL==pinfile) return;
  5.     TCHAR szTest[MAX_PATH]=_T("");
  6.     while (!feof(pinfile))
  7.     {
  8.         if (NULL!=_fgetts(szTest, MAX_PATH, pinfile))
  9.             MessageBox(NULL, szTest, _T("debug"), 0);
  10.         else
  11.             MessageBox(NULL, _T("NULL"), _T("debug"), 0);
  12.     }
  13.     fclose(pinfile);
  14. }
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

结果:弹出两个对话框,内容分别为“1”和“NULL”。
分析:以上结果与测试4的分析是一致的。

 

 

测试6. 文件内容不变,使用下面程序测试

隐藏行号 复制代码 测试代码2。
  1. void TestFun4()
  2. {
  3.     FILE *pinfile = _tfopen(_T("f://12.txt"), _T("rt+"));
  4.     if (NULL==pinfile) return;
  5.     TCHAR szTest[MAX_PATH]=_T("");
  6.     while (NULL!=_fgetts(szTest, MAX_PATH, pinfile))
  7.     {
  8.         MessageBox(NULL, szTest, _T("debug"), 0);
  9.     }
  10.     fclose(pinfile);
  11. }
.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}.number_show{ padding-left:52px !important; list-style:decimal outside !important}.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}.number_hide{ padding-left:0px !important; list-style-type:none !important}.number_hide li{ list-style-type:none !important; border-left:0px}ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}ol.mainarea li pre.alt{ background-color:#f7f7ff !important}结果:只弹出一个对话框,内容为“1”。

 

总结:所以,我们在整行得去文件的时候,最好采用fgets函数的返回值来控制while循环而不是采用fefo函数。

原创粉丝点击