回 帖 发 新 帖 刷新版面

主题:C语言问题,希望有人 能给解答下

C语言读取 文本内IP 段的问题  先看代码
[code=c]
#include "stdio.h"
#include "stdlib.h"

main()

{
      int n;
      char *startip[200],*endip[200];
      FILE *fp;
      fp=fopen("ip.txt","r");
      for(n=0;!feof(fp);n++)
      {
          fscanf(fp,"%s %s\n",&startip[n],&endip[n]);
          printf("%s %s\n",&startip[n],&endip[n]);                  
          }
      fclose(fp);
      system("pause");


}
[/code]
代码大致就是这样的 定义的指针数组 来循环读取每一行的IP 段
IP 段的排列 如下
192.168.0.1 192.168.0.255
192.168.1.1 192.168.1.255
可是定义指针数组 必须要表明数组长度   晕死 但是用变量n 来声明的话 后面的for 循环中n=0开始的    在VC tc  都是不能编译通过的
 我要的是无限循环读取的   这个 假若  ip.txt中的ip段超过200段的 话 程序 运行就会出错
如何 修改 代码 以达到 无限循环读取呢



PS:这个如果定义char *startip[n]  后面n =0 在devc++ 4.992 能通过 运行也正常 ,也就是可以无限读取,但是 在wxdevc++7.02  下能通过编译 但是运行 有问题 就是 不能读取IP 段

回复列表 (共14个回复)

沙发

程序有错,for循环下的复合语句出错了。


如果你只想读取IP并打印出来,而不保存在startip数组里和endip数组里的话,你完全可以定义startip和endip为一维数组,每次读取的IP段都保存在这两个数组里并打印,(为保证IP的正确性,第二次循环时需清空两个数组);也可以定义startip和endip指针,每次读取前malloc分配空间。

板凳

C语言 我梦想的语言

LZ怎么不回复了

2楼说的对不对啊

我也学习一下

3 楼

[quote]程序有错,for循环下的复合语句出错了。


如果你只想读取IP并打印出来,而不保存在startip数组里和endip数组里的话,你完全可以定义startip和endip为一维数组,每次读取的IP段都保存在这两个数组里并打印,(为保证IP的正确性,第二次循环时需清空两个数组);也可以定义startip和endip指针,每次读取前malloc分配空间。[/quote]
来个具体点的代码   , 我也是刚学的   ,对于 内存这块 我也是 比较晕的  不是太懂硬件 等C  学学 在 去了解下汇编     我也用结构体来 读取和保存 ip的   看代码[code=c]
#include "stdio.h"
#include "stdlib.h"

struct IP
{
       unsigned int p1;
       unsigned int p2;
       unsigned int p3;
       unsigned int p4;
       };
struct IP startip,endip;          
main()
{
      FILE *fp;
      fp = fopen("ip.txt","r");
      while(!feof(fp))
      {
          fscanf(fp,"%d.%d.%d.%d %d.%d.%d.%d\n",&startip.p1,&startip.p2,&startip.p3,&startip.p4,&endip.p1,&endip.p2,&endip.p3,&endip.p4);
          printf("%d.%d.%d.%d %d.%d.%d.%d\n",startip.p1,startip.p2,startip.p3,startip.p4,endip.p1,endip.p2,endip.p3,endip.p4);
      }
      fclose(fp);
      system("pause");
      }

[/code]
 可是这个问题又出来了啊    这个读取和打印 都是没有问题的   就是要使用这个IP 的话     ip 已经被拆开了啊   在 连接起来 就太麻烦了啊  我想简单点啊    

4 楼


汗  刚才 又试了下  原来的代码 稍微修改下  用你所说的 定义个一维数组   这次倒是没有问题了啊   [code=c]
请填写代码
[/code][code=c]
#include "stdio.h"
#include "stdlib.h"
#include "mem.h"

main()
{
    char startip[30],endip[30];
    FILE *fp;
    fp = fopen("ip.txt","r");
    while(!feof(fp))
    {
    fscanf(fp,"%s %s\n",startip,endip);
    printf("%s %s\n",startip,endip);
    memset(startip,0,sizeof(startip));
    memset(endip,0,sizeof(endip));
    }
    fclose(fp);
    system("pause");
}
[/code]
去掉memset  貌似 也是可以的 没问题 可以无限循环读取   加上 这俩句   效果都是一样的   不过还是很想了解下  用动态分配内存的方式达到  这种效果的  
  希望你 不要吝啬的 说下啊  谢啦

5 楼

恩,的确不需要清空,它会自动给一个结束标志,不需考虑前一次的影响。那第二种方法也就没必要每次都分配了,一次就行。哎~~~~~~~~~,疏忽了。

6 楼

放到char型中的指针中不久得了吗~!干嘛还定int

7 楼

[quote]恩,的确不需要清空,它会自动给一个结束标志,不需考虑前一次的影响。那第二种方法也就没必要每次都分配了,一次就行。哎~~~~~~~~~,疏忽了。[/quote]

 汗 来段代码来看看饿

8 楼


汗  刚自己又写了个  感觉问题很大  
[code=c]
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"


main()
{
    char *startip,*endip;
    FILE *fp;
    if (((startip = malloc(20))==NULL) && ((endip = malloc(20)) == NULL))    
    {
        printf("Not enough memory to allocate buffer!");
        exit(1);
        }
    fp = fopen("ip.txt","r");
    while(!feof(fp))
    {
        fscanf(fp,"%s %s\n",&startip,&endip);
        printf("%s %s\n",&startip,&endip);
        }
        fclose(fp);
        free(startip);
        free(endip);
        system("pause");
    }

[/code]
编译能通过  不过VC 下  运行会 出问题  貌似 能读取  也能不断循环的读取  
 不过有个巨大的问题
  如果 是 这样格式的ip 段的话
192.168.0.1 192.168.0.255
192.168.1.1 192.168.1.255
读取后打印会变这样
168.0.1 192.168.0.255
168.1.1 192.168.0.255
  出了什么问题 

9 楼

汗 在补充下 用VC debug 模式编译的运行会出问题 但release 下编译的不会出问题 
但运行效果 恰好和devc++ 编译 的相反 VC  运行的效果会是这样
192.192.168.0.255 192.168.0.255
192.192.168.1.255 192.168.1.255
192.192.168.2.255 192.168.2.255
 前面多了个    devc++ 是 少了个   我晕死

10 楼

改了一下
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"


main()
{
    char *startip,*endip;
    FILE *fp;
 if (((startip =(char *) malloc(20))==NULL) || ((endip = (char *)malloc(20)) == NULL)) //有一个为空,就输出错误信息,而且用与时一般情况下第二个是不会做的
  {
        printf("Not enough memory to allocate buffer!");
        exit(1);
        }
    fp = fopen("C:\\ip.txt","r");
    while(!feof(fp))
    {
        fscanf(fp,"%s %s\n",startip,endip);//已经是地址了,不需加取地址符号
        printf("%s %s\n",startip,endip);
        }
        fclose(fp);
        free(startip);
        free(endip);
        system("pause");
  }
    

我来回复

您尚未登录,请登录后再回复。点此登录或注册