一百分求一C小程序

momuz 2004-08-11 03:42:36
给定一个IP,然后在路由表中查找相应网段,函数返回该网段及掩码,假设路由表是文件格式,例如一条记录为:162.105.1.0 255.255.255.0

给出代码调试通过即给分,有点急,请各位帮帮我,谢谢!
...全文
512 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
momuz 2004-08-13
  • 打赏
  • 举报
回复
我晕倒了,发错分了!!
momuz 2004-08-12
  • 打赏
  • 举报
回复
楼上的可以用VC通过,但IP是代码中指定了的,要求用户输入来判断,另外是查找输入IP所在的网段及掩码,不是完全匹配,也没返回网段

唉,总之还是要谢谢了,还有其它办法吗?
Steel1010 2004-08-12
  • 打赏
  • 举报
回复
FILE * pf=fopen("C:\\ip.txt","r"); 也能通过

// ReadIp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream.h>
#include "iostream.h"
#include "stdio.h"
#include <string.h>

bool Find(char* ,char* );



int main(int argc, char* argv[])
{
char Mask[64];
memset(Mask,0x00,64);
if (Find("61.128.128.68",Mask) == true)
{
printf("%s \n", Mask);
}
else
{
printf("\n failed to get!");
}
return 0;

}
bool Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
memset(tmp,0x00,50);
FILE * pf=fopen("C:\\ip.txt","r");
while (fgets(tmp,128,pf)!=NULL)
{
if(strncmp(IP,tmp,IpLen)==0)//找到
{
strcpy(MaskBuf,tmp+IpLen+1);
return true;
}
}
return false;
}

Output:
255.255.255.12
///:~
Steel1010 2004-08-12
  • 打赏
  • 举报
回复
帮助 lemon520(喷血)测了一下,在VC6.0 win32 console application 下好使,如下:
// ReadIp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream.h>
#include "iostream.h"
#include "stdio.h"
#include <string.h>

bool Find(char* ,char* );



int main(int argc, char* argv[])
{
char Mask[64];
memset(Mask,0x00,64);
if (Find("61.128.128.68",Mask) == true)
{
printf("%s \n", Mask);
}
else
{
printf("\n failed to get!");
}
return 0;

}
bool Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
memset(tmp,0x00,50);
ifstream inf("c:\\ip.txt"); //这里的文件名你自己改成你的文件。
while (!inf.eof())
{
inf.getline(tmp,128);
if(strncmp(IP,tmp,IpLen)==0) //找到
{
strcpy(MaskBuf,tmp+IpLen+1);//tmp+IpLen+1 = tmp[0 + IpLen + 1]
return true;
}
}
return false;
}
ip.txt中的数据为:

162.105.1.3 255.255.255.0
162.105.1.2 255.255.255.0
162.105.1.1 255.255.255.0
162.105.1.0 255.255.255.0
61.128.128.68 255.255.255.12

output: 255.255.255.12
momuz 2004-08-11
  • 打赏
  • 举报
回复
先不结贴吧,结后一起给分,谢谢
momuz 2004-08-11
  • 打赏
  • 举报
回复
真的不好意思啊,先送上50分,不过还是有问题,改过后的这段代码可编译:
#include <stdio.h>
#include <string.h>

int Find(char*,char*);

void main()
{
char IP[20],Mask[20],Subnet[20];

memset(IP,0x00,20);
memset(Mask,0x00,20);
memset(Subnet,0x00,20);
printf("enter IP:\n");
scanf("%s",IP);
if(Find(IP,Mask))
{
printf("subnet is %s,mask is%s",Subnet,Mask);
return ;
}
printf("error!");

}

int Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
FILE * pf = fopen("f:\\ip.txt","r");
memset(tmp,0x00,50);
while(fgets(tmp,128,pf)!=NULL)
{
if(strncmp(IP,tmp,IpLen)==0)
{
strcpy(MaskBuf,tmp+IpLen+1);
fclose(pf);
return 1;
}
}
fclose(pf);
return 0;
}

但无法返回IP段及掩码
gordenfl 2004-08-11
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <fstream>

int Find(char*,char*);

void main()
{
char IP[20],Mask[20];

memset(IP,0x00,20);
memset(Mask,0x00,20);
printf("请输入你要查找的IP:\n");
scanf("%s",IP);
if(Find(IP,Mask))
{
printf("掩码为%s",Mask);
return ;
}
printf("未找到!");

}

int Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
memset(tmp,0x00,50);
FILE * pf=fopen("C:\\ip.txt","r"); //这里改为你的文件名
while(fgets(tmp,128,pf)!=NULL)
{
if(strncmp(IP,tmp,IpLen)==0)//找到
{
strcpy(MaskBuf,tmp+IpLen+1);
fclose(pf);
return 1;
}
}
fclose(pf);
return 0;
}
lemon520 2004-08-11
  • 打赏
  • 举报
回复
我受不了了!
Leaveye 2004-08-11
  • 打赏
  • 举报
回复
#include <stdio.h>

int Find(char*,char*);

void main()
{
char IP[20],Mask[20];

memset(IP,0x00,20);
memset(Mask,0x00,20);
printf("请输入你要查找的IP:\n");
scanf("%s",IP);
if(Find(IP,Mask))
{
printf("掩码为%s",Mask);
return ;
}
printf("未找到!");

}

int Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
FILE * pf = fopen("C:\\ip.txt","r"); //这里改为你的文件名
memset(tmp,0x00,50);
while(fgets(tmp,128,pf)!=NULL)
{
if(strncmp(IP,tmp,IpLen)==0)//找到
{
strcpy(MaskBuf,tmp+IpLen+1);
fclose(pf);
return 1;
}
}
fclose(pf);
return 0;
}

楼主请再调试一下吧。别忘记换文件名
momuz 2004-08-11
  • 打赏
  • 举报
回复
改了下代码, FILE *pf;单独一行,可以运行,但无论输入什么都没有结果返回
momuz 2004-08-11
  • 打赏
  • 举报
回复
--------------------Configuration: IP - Win32 Debug--------------------
Compiling...
IP.C
c:\turboc2\ip.c(28) : error C2275: 'FILE' : illegal use of this type as an expression
d:\program files\microsoft visual studio\vc98\include\stdio.h(156) : see declaration of 'FILE'
c:\turboc2\ip.c(28) : error C2065: 'pf' : undeclared identifier
c:\turboc2\ip.c(29) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
c:\turboc2\ip.c(29) : warning C4024: 'fgets' : different types for formal and actual parameter 3
c:\turboc2\ip.c(34) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
c:\turboc2\ip.c(34) : warning C4024: 'fclose' : different types for formal and actual parameter 1
c:\turboc2\ip.c(38) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
c:\turboc2\ip.c(38) : warning C4024: 'fclose' : different types for formal and actual parameter 1
Error executing cl.exe.

IP.OBJ - 2 error(s), 6 warning(s)


VC还是有问题,我要晕倒了
  • 打赏
  • 举报
回复
mark
lemon520 2004-08-11
  • 打赏
  • 举报
回复
我没有TC
你用VC编译吧
momuz 2004-08-11
  • 打赏
  • 举报
回复
谢谢,我用TC编译有两个错误,三个警告:
1.FILE * pf=fopen("C:\\ip.txt","r"); 此行报:improper use of a typedef symbol in function Find
2.还是这行:Undefined symbol 'pf' in function Find
3-5.while(fgets(tmp,128,pf)!=NULL)
fclose(pf);
另外倒数第三行的fclose(pf);
此三行警告:Non-protable pointer conversion in function Find

怎么改正啊?再求
lemon520 2004-08-11
  • 打赏
  • 举报
回复
刚才那个多了一个东西

#include <fstream> //这行要去掉
lemon520 2004-08-11
  • 打赏
  • 举报
回复
纯C的如下

#include <stdio.h>
#include <fstream>

int Find(char*,char*);

void main()
{
char IP[20],Mask[20];

memset(IP,0x00,20);
memset(Mask,0x00,20);
printf("请输入你要查找的IP:\n");
scanf("%s",IP);
if(Find(IP,Mask))
{
printf("掩码为%s",Mask);
return ;
}
printf("未找到!");

}

int Find(char* IP,char* MaskBuf)
{
int IpLen=strlen(IP);
char tmp[128];
memset(tmp,0x00,50);
FILE * pf=fopen("C:\\ip.txt","r"); //这里改为你的文件名
while(fgets(tmp,128,pf)!=NULL)
{
if(strncmp(IP,tmp,IpLen)==0)//找到
{
strcpy(MaskBuf,tmp+IpLen+1);
fclose(pf);
return 1;
}
}
fclose(pf);
return 0;
}
momuz 2004-08-11
  • 打赏
  • 举报
回复
用VC编译报错啊
micty 2004-08-11
  • 打赏
  • 举报
回复
看了算法改一下便可以了吧
momuz 2004-08-11
  • 打赏
  • 举报
回复
谢谢 lemon520 但我要的是C程序,要命令行执行即可,能改成C的么?
ttm1984 2004-08-11
  • 打赏
  • 举报
回复
先记下再说~
------------------------------------
技术才是
加载更多回复(4)

69,369

社区成员

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

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