当前位置: 技术问答>linux和unix
一个读取文件内容时遇到的问题,就是只读取了文件第一行的内容。。
来源: 互联网 发布时间:2016-06-27
本文导语: #include #include #include int main(void) { FILE *fp; fp = fopen("/spms/idep/log/ftp_client/log/collect_20090815_00031.list","r"); if(NULL == fp) { printf("Open file failednn"); } char path[4096]; if( fgets(path,4096,fp) == NULL ) { ...
#include
#include
#include
int main(void)
{
FILE *fp;
fp = fopen("/spms/idep/log/ftp_client/log/collect_20090815_00031.list","r");
if(NULL == fp)
{
printf("Open file failednn");
}
char path[4096];
if( fgets(path,4096,fp) == NULL )
{
printf("Failed nn");
}
else
{
printf("The path is %snn",path);
printf("The length of the path :is %d n",strlen(path));
}
return 0;
}
我文件当中存的内容是:
- 23 20090827160103 NIC_20090826020.0131^M
- 23 20090827155524 NIC_20090826020.0129^M
- 23 20090827145257 NIC_20090826020.0128^M
- 23 20090827145257 NIC_20090826020.0127^M
- 23 20090827145256 NIC_20090826020.0126^M
- 23 20090827145118 NIC_20090826020.0100^M
- 23 20090827145254 NIC_20090826020.0125^M
- 23 20090827145254 NIC_20090826020.0124^M
- 23 20090827145253 NIC_20090826020.0123^M
- 23 20090827145252 NIC_20090826020.0122^M
- 23 20090827145251 NIC_20090826020.0121^M
- 23 20090827145249 NIC_20090826020.0120^M
- 23 20090827145239 NIC_20090826020.0119^M
- 23 20090827145238 NIC_20090826020.0118^M
- 23 20090827145237 NIC_20090826020.0117^M
- 23 20090827145236 NIC_20090826020.0116^M
- 23 20090827145234 NIC_20090826020.0115^M
- 23 20090827145233 NIC_20090826020.0114^M
- 23 20090827145232 NIC_20090826020.0113^M
- 23 20090827145230 NIC_20090826020.0112^M
- 23 20090827145228 NIC_20090826020.0111^M
- 23 20090827155839 NIC_20090826020.0130^M
- 23 20090827145223 NIC_20090826020.0110^M
- 23 20090827145211 NIC_20090826020.0109^M
- 23 20090827145209 NIC_20090826020.0108^M
- 23 20090827145205 NIC_20090826020.0107^M
- 23 20090827145204 NIC_20090826020.0106^M
- 23 20090827145203 NIC_20090826020.0105^M
- 23 20090827145202 NIC_20090826020.0104^M
- 23 20090827145201 NIC_20090826020.0103^M
- 23 20090827145159 NIC_20090826020.0102^M
- 23 20090827145156 NIC_20090826020.0101^M
我想把它全部读取出来,但是上面的程序只能读取出第一行信息。我该怎办呢?????
|
int main(void) {
FILE *fp;
fp = fopen("/home/test/fgets.c","r");
if(NULL == fp)
{
printf("Open file failednn");
}
char path[4096];
while( fgets(path,4096,fp) != NULL )
{
printf("%s",path);
}
return 0;
}
FILE *fp;
fp = fopen("/home/test/fgets.c","r");
if(NULL == fp)
{
printf("Open file failednn");
}
char path[4096];
while( fgets(path,4096,fp) != NULL )
{
printf("%s",path);
}
return 0;
}
|
你输出的信息要放到什么位置吧! 再说文件也没关闭!
|
fgets只读一行~~
写个循环就可以了
写个循环就可以了
|
loop
|
你的循环呢
fgets可是读到换行符就结束的
fgets可是读到换行符就结束的