c语言读出文本指定字符串

skyctr 2009-03-18 04:21:57
一配置文件:

# check if layer2 module is running, if not start it.

../../layer2 -I1 -h10.8.8.69 -p4000 &
sleep 10
echo "LAYER2 module started"
echo
echo
echo "PCU IN SERVICE"
echo

如何用C读取其中的IP地址(10.8.8.69 )和端口地址(4000)
高分答谢!!
...全文
100 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
magipan 2009-03-18
  • 打赏
  • 举报
回复
找到标志后用格式读取?
kingofice 2009-03-18
  • 打赏
  • 举报
回复
sscanf正道
baihacker 2009-03-18
  • 打赏
  • 举报
回复
正想来说sscanf的...
  • 打赏
  • 举报
回复
up下
skyctr 2009-03-18
  • 打赏
  • 举报
回复
感谢 wuyu637 lingyin55 Dinelgua 的回答
期待大家都参与讨论,我的方法是采用sscanf()来过滤-i -h -p
chuengchuenghq 2009-03-18
  • 打赏
  • 举报
回复
学习
Dinelgua 2009-03-18
  • 打赏
  • 举报
回复

char buff[] = "../../layer2 -I1 -h10.8.8.69 -p4000 &"

char strIP[20];
char strPort[10];

char * pdest = NULL;
pdest = memchr(buff,'-',sizeof(buff))
if(NULL != pdest)
{
if(('h'== (*(pdest+1)))//说明接下来是IP
{
char pPort = memchr(pdest+1,'-',(sizeof(buff)-(pdest-buff)));
memcpy(strIP,pdest+1,(pPort-pdest-1));//ip就拷贝完了 port楼主自己参照写一下就行了
}
}
zsf81 2009-03-18
  • 打赏
  • 举报
回复
学习
lingyin55 2009-03-18
  • 打赏
  • 举报
回复

#include<iostream>
#include<conio.h>
#include<string>
#include<stdlib.h>
using namespace std;

int Count=0;

/*
*描述:如果存在则返回字符串所在的位置,否则返回0,暂不支持文本中存在多个相同的串
* 先用test.txt文本测试,所有的文本操作都是一样的,不管你怎么命名
*/
int findNum(char *str)
{
FILE *p;
if((p=fopen("test.txt","rb"))==NULL)
{
printf("\n打开文件失败\n");
return 0;
}
char buffer[0x1000]; //保存文件
memset(buffer,0,0x1000); //初始化缓存
size_t fileLen=fread(buffer,sizeof(char),0x1000,p); //得到文件内容,
int readLen=strlen(str);
int IsFind=0;

for(int i=0;i<fileLen;i++)
{
if(strncmp(buffer+i,str,readLen)==0)
{
IsFind=i;
}
}

fclose(p);
return IsFind;
}

int main(void)
{
char *str1="1234567";
int t1=0,t2=0;
if((t1=findNum(str1))==0)
{
printf("没有找到字符串%s\n请按任意键退出\n",str1);
return -1;
}
else
{
printf("字符串%s的位置在%d\n",str1,t1);
}
return 0;
}
wuyu637 2009-03-18
  • 打赏
  • 举报
回复
while(getline (strs))
{
if(strstr(strs,"-h"))
{
strtok( -h);
turn string to ip number
strtok( -p);
turn string to port number
}
else
{
do nothing
}
}

69,375

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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