C语言读取文件内容,按行读

循环中用fgets函数

fgets读取一行文件内容到字符数组

并把文件指针指向下一行

用法如下

#include

int main()
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL)

perror ("Error opening file");
   else {

while( fgets (mystring , 100 , pFile)!= NULL )

{
         printf ("%s\n",mystring);
 }

    fclose (pFile);
   }
   return 0;
}

你可能感兴趣的:(c或c++专栏)