用fgets( char *string, int n, FILE *stream );Sample:
FGETS.C/* 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[MAX_STRING];   if( (stream = fopen( "fgets.c", "r" )) != NULL )
   {
      if( fgets( line, MAX_STRING, stream ) == NULL)
         printf( "fgets error\n" );
      else
         printf( "%s", line);
      fclose( stream );
   }
}
输出(得到文件第一行):/* FGETS.C: This program uses fgets to display
 

解决方案 »

  1.   

    楼上用的是C,可以用。用CFILE类有没有办法?(我只能一个一个读入,然后用“\r\n”来测试一行结束)
      

  2.   


    FILE *stream;
    if(getFileName(stream)!=1)
    {
                     ........
             }

    // 读取文件数据
    char strData[255]; 
    for(; !feof(stream) ; )
    {
    if(fscanf( stream, "%s", &strData)!=0)
                      // 读入一行  注意 strData 的长度
    {
                           // do my operation
    }
    }
        fclose( stream );
      

  3.   

    CFILE的READ()是不能按行读取得,但可以检验是否读到"\n",一次判断是否一行结束,最好还是用 ReadString() 
    CStdioFile pFile;
    CString str;
    pFile.Open("文件名","CFile::modeRead");
    ……
    pFile.ReadString(str);str就是读取的一行字符,如果想一行一行的读取直到文件尾,可以判断ReadString()的返回值(返回值为布尔值):
    while(pFile.ReadString(str))
    {
    ……
    }