linux 读取逐行txt文件

Takuson 2011-12-13 03:03:14
有一个文本文件“ip.txt”,内容有两行:
ip:127.0.0.1
port:4567

怎么把这两行分别读出到buf1和buf2呢? 请各位朋友指教
...全文
463 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanjinbin 2011-12-13
  • 打赏
  • 举报
回复
使用fgets()函数.
askandstudy 2011-12-13
  • 打赏
  • 举报
回复
可能有很多种方法吧,从我以前的代码里抠了一段组装了一下,看看也还能运行,你看看是不是这样:
不过这些函数我都不记得那些参数具体是什么意思了,你自己网上查一下看看。


[root@RHEL6A ccode]# cat readfile.c
#include <stdio.h>
int ReadIniFile(char* strFile);

int main(int argc, char* argv[])
{
int iRet;
iRet=readfile("/tmp/msisdn.txt");
}


int readfile(char* strFile)
{
FILE *fp;
char cLineBuffer[100];


fp=fopen(strFile,"r");
if(fp == NULL)
{
printf("can not open the config file %s",strFile);
return -1;
}

while(fgets(cLineBuffer,100,fp)!=(char *) NULL)
{
printf("%s",cLineBuffer);
}
fclose(fp);
return 0;
}
[root@RHEL6A ccode]# cat /tmp/msisdn.txt
2001
2002
2003

[root@RHEL6A ccode]# gcc -o readfile.o readfile.c
[root@RHEL6A ccode]# ./readfile.o
2001
2002
2003

Takuson 2011-12-13
  • 打赏
  • 举报
回复
我想用C语言,不是shell脚本的^^
askandstudy 2011-12-13
  • 打赏
  • 举报
回复
前几天才回了个贴,你是不是要的这个功能呢?
http://topic.csdn.net/u/20111209/15/31620fa8-8540-4133-ad8f-1ade4a054b59.html

但是你这里的是冒号

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧