user-0 提问: 为什么无法读取到txt文件数据

回复:
user: user-1
content: 另外，fgets是一次读一行，还要确认一下源文件的每行是否超过11个字节，否则会内存泄漏吧； fputs同理；

user: user-1
content: 缓存是11个字节，你要读12个字节进去；这儿是矛盾的；所以，建议你查看读写函数返回值，即判断是否出错；另外，确认从源文件读到的数据；

user: user-2
content: fgets, fgetws Get a string from a stream.  char *fgets( char *string, int n, FILE *stream );  wchar_t *fgetws( wchar_t *string, int n, FILE *stream );  Function Required Header Compatibility  fgets <stdio.h> ANSI, Win 95, Win NT  fgetws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.  Parameters  string  Storage location for data  n  Maximum number of characters to read  stream  Pointer to FILE structure  Remarks  The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.   fgets is similar to the gets function; however, gets replaces the newline character with NULL. fgetws is a wide-character version of fgets.   fgetws reads the wide-character argument string as a multibyte-character string or a wide-character string according to whether stream is opened in text mode or binary mode, respectively. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _fgetts fgets fgets fgetws    Example  /* 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 );    } }   Output  /* FGETS.C: This program uses fgets to display     Stream I/O Routines  See Also   fputs, gets, puts

user: user-2
content: #include <stdio.h> int main() {     FILE *fp1,*fp2;     unsigned char ch[11];      if((fp1=fopen("D:\\test.txt","r"))==NULL) {         printf("Can't open file D:\\test.txt\n");         return 1;     }     if((fp2=fopen("D:\\z.txt","w"))==NULL) {         fclose(fp1);         printf("Can't open file D:\\z.txt\n");         return 2;     }     while(1) {         if (NULL==fgets(ch,11,fp1)) break;         fputs(ch,fp2);     }     fclose(fp2);     fclose(fp1);     return 0; }

user: user-3
content: 把         unsigned char ch[11];     int num=12; 改成     unsigned char ch[12];     int num=11;  原因嘛，     fgets(ch,num,fp1);//读取一个数据帧 能造成缓冲区溢出

user: user-4
content: ch数组大小设置的是不是太小，也没有清零，每次读写文件后ch数组都清零

user: user-5
content: fgets函数，参数s是缓冲区的首地址，size是缓冲区的长度，该函数从stream所指的文件中读取以'\n'结尾的一行（包括'\n'在内）存到缓冲区s中，并且在该行末尾添加一个'\0'组成完整的字符串。 fgets()函数中，第一个参数指针需要我们事先分配好大小。另外，fgets()函数不适合取二进制的文件，这样，容易发生错误。  注：fgets从指定的文件中读一行字符到调用者提供的缓冲区中，gets从标准输入读一行字符到调用者提供的缓冲区中。 1.当遇到换行符或者缓冲区已满，fgets就会停止，返回读到的数据，值得注意的是不能用fgets读二进制文件，因为fgets会把二进制文件当成文本文件来处理，这势必会产生乱码。 2.每次调用，fgets都会把缓冲区的最后一个字符设为null，这意味着最后一个字符不能用来存放需要的数据，所以如果有一行，含有LINE_SIZE个字符(包括换行符)，要想把这行读入缓冲区，请把参数n设为LINE_SIZE+1

user: user-6
content: 用二进制读写文件好点。