user-0 提问: fgets函数出问题，求解

回复:
user: user-1
content: 程序没有问题，新建记事本文件，可以正常打印输出。  也有可能是人品问题。。。。

user: user-2
content: 目测fgets看到BOM就直接return null了

user: user-3
content: 打开文件的地方“r”，换成“rb” 注意确认打开的文件是否为unicode码文件，看错误，这个的可能性不大。 这个错误，比较怪，看你的代码似乎不应当出这种问题。 除非，文件内容只有1行。是不是这样？ 因为你的程序中，只输出偶数行。。

user: user-4
content: In Visual C++ 2005, fopen supports Unicode file streams. A flag specifying the desired encoding may be passed to fopen when opening a new file or overwriting an existing file, like this:  fopen("newfile.txt", "rw, ccs=<encoding>");  Allowed values of the encoding include UNICODE, UTF-8, and UTF16-LE. If the file is already in existence and is opened for reading or appending, the Byte Order Mark (BOM) is used to determine the correct encoding. It is not necessary to specify the encoding with a flag. In fact, the flag will be ignored if it conflicts with the type of the file as indicated by the BOM. The flag is only used when no BOM is present or if the file is a new file. The following table summarizes the modes used in for various flags given to fopen and Byte Order Marks used in the file.  Flag  No BOM (or new file)  BOM: UTF-8  BOM: UTF-16   UNICODE  ANSI  UTF-8  UTF-16LE   UTF-8  UTF-8  UTF-8  UTF-16LE   UTF-16LE  UTF-16LE  UTF-8  UTF-16LE    If mode is "a, ccs=<encoding>", fopen will first try to open the file with both read and write access. If it succeeds, it will read the BOM to determine the encoding for this file; however, if it fails, it will use the default encoding for the file. In either case, fopen will then re-open the file with write-only access. (This applies to mode a only, not a+.)

user: user-0
content: 引用 9 楼 FancyMouse 的回复:目测fgets看到BOM就直接return null了那如果是处理这种文本文件该如何处理呢？

user: user-4
content: #include <stdio.h> #include <stdlib.h> #define MAXLEN 1000 int main() {     int i = 0;     char buff[MAXLEN];     FILE *F;     F = fopen("Speaker0830.txt","r");     if (F == NULL)     {         printf("Can't find file.txt\n");     }     else     {         while (1)         {             if (NULL==fgets(buff,MAXLEN,F)) break;             i++;             printf("%d: %s",i,buff);         }         fclose(F);     }     system("PAUSE");     return 0; }

user: user-5
content: 目测是楼主在调试的程序时候，路径Speaker0830.txt位置不对吧。  如果自己手动调试的模式下的话文件必须放在工程代码文件目录下面。  直接运行则放在对应exe目录下面。  仔细检查吧。

user: user-6
content: 程序没有错，看一下Speaker0830.txt里面是什么东西，是不是可见字符？能不能打印出来？ 自己写一个简单的txt文件。用绝对路径试试就知道了。

user: user-7
content: MSDN示例，可以参考增加fget返回值判断调试一下：  /* FGETS.C: This program uses fgets to display  * a line from a file on the screen.  */  #include <stdio.h>  void main( void ) {    FILE *stream;    char line[100];     if( (stream = fopen( "fgets.c", "r" )) != NULL )    {       if( fgets( line, 100, stream ) == NULL)          printf( "fgets error\n" );       else          printf( "%s", line);       fclose( stream );    } }

user: user-0
content: 引用 4 楼 lovesmiles 的回复:程序没有错，看一下Speaker0830.txt里面是什么东西，是不是可见字符？能不能打印出来？ 自己写一个简单的txt文件。用绝对路径试试就知道了。有内容，是可见字符。今天把问题找到了，但是不能理解。我将文本另存为ANSLL就能读了，之前是UTF-8，很奇怪。用UTF-8的格式保存时，二进制读取都不行！求解释

user: user-0
content: 引用 2 楼 blueroger07 的回复:程序没错吧,确定Speaker0830.txt里面有内容?有内容，今天把问题找到了，但是不能理解。我将文本另存为ANSLL就能读了，之前是UTF-8，很奇怪。用UTF-8的格式保存时，二进制读取都不行！

user: user-8
content: 程序没错吧,确定Speaker0830.txt里面有内容?

user: user-9
content: 文件和数组的参数位置搞反了