c语言一行一行的读取文件

我写的是linux下的程序,一个文件叫MYFILE里面是一行一行的写内容的,我现在想一行一行的读取出来,请问怎么写?

[10:30:09 @cfiles]$ cat test8.c
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 100
int main()
{

FILE *fp;
char arr[MAXLINE+1];

if ((fp = fopen ("MYFILE", "r")) == NULL)
{
perror ("File open error!\n");
exit (1);
}
while ((fgets (arr, MAXLINE, fp)) != NULL)
{
fputs (arr, stdout);
}
return 0;
}
[10:30:35 @cfiles]$ gcc test8.c
[10:30:40 @cfiles]$ ./a.out
abcdef
sdkpn
love
[10:30:52 @cfiles]$ cat MYFILE
abcdef
sdkpn
love
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-31
using namespace std;
.....

string preline(""), curline("");
ifstream infile("file name");

while(getline(infile, curline))
{
.....

// 得前一行
if(preline.length() > 0) {
..... // preline 为前一行数据
}

.....

preline = curline;
}

infile.close();
第2个回答  2011-03-31
使用标准c++完全跨平台!!
第3个回答  2011-03-31
是执行单步运行吧?
相似回答