Numbers issue...

This is a discussion on Numbers issue... within the C Programming forums, part of the General Programming Boards category; Hello all, Basically, I've been scouring google and all the obscure sites therein.... I can't understand why this is happening. ...

  1. #1
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94

    Question Strange things happening, maybe demons?

    Hello all,

    Basically, I've been scouring google and all the obscure sites therein.... I can't understand why this is happening. See, the below code code out and nabs values through snmp. I'm sorry the code isn't pretty, but I am not (yet?) a very good c programmer. So, after it has those values, it calls some sytem events that don't really matter. (It's rrdtool, if you'd like to know more, pm me. Way cool stuff.) Anyway, my problem is that when I get the values eth0in,eth0out, and loout, I have them as strings. And I can't seem to convert them to anything else, and I need to do math things with them. I've tried integer, long int, double, float, etc, using their respective conversion functions. But the number I get out is never the same. I'm thinking that maybe there's some funky memory values floating around near the string?

    I really don't know. Again, sorry that this code isn't pretty.

    UPDATED: alright, basically when one runs this code, they see the string version and the long int version of the numbers. You will see if you run it... but If you don't this is what the result is:

    Code:
    eth0in    625163572    625163572
    eth0out 17025541       17025541
    lo           3045189694   2147483647 //<<<WHAT IS THAT???
    
         >>>eth0 in 0 bps
         >>>eth0 out 0 bps
         >>>lo 0 bps
    Notice how in the lo section, the string and the long int values are different. WHAT have I done? I need an exorcist!

    Here is the code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<strings.h>
    #include<time.h>
    
    char *rip (char *s)
    {
       char *p;
    
       if ((p = strrchr(s,'\n')) != NULL) *p = '\0';
       if ((p = strrchr(s,'\r')) != NULL) *p = '\0';
       return (s);
    }
    
    int main()
    {
       FILE *f;
       char buf[100];
       char tmp[16];
       char cmd[100];
       char host[50],commstr[30];
       long int eth0in,eth0out,lo,itmp,etime;
       long int peth0in,peth0out,plo,petime;//previous values
       short int first=0;
    
       strncpy(host,"localhost",sizeof(host));
       strncpy(commstr,"public",sizeof(commstr));
    
       sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifInOctets.3 >/tmp/sysgrapheth0in",host,commstr);
       system(cmd);
       sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifOutOctets.3 >/tmp/sysgrapheth0out",host,commstr);
       system(cmd);
       sprintf(cmd,"snmpwalk %s %s interfaces.ifTable.ifEntry.ifOutOctets.1 >/tmp/sysgraphlooutout",host,commstr);
       system(cmd);
    
       system("perl time.pl >/tmp/sysgraphtime");
       system("/sbin/clock >/tmp/sysgraphclock");
    
       f = fopen("/tmp/sysgraphtime","r");
       fgets(buf,sizeof(buf),f);
       fclose(f);
    
       etime = atoi(buf);
    
       f = fopen("/tmp/sysgrapheth0in","r");
           fgets(buf,sizeof(buf),f);
       fclose(f);
    
       tmp[0] = buf[53];
       tmp[1] = buf[54];
       tmp[2] = buf[55];
       tmp[3] = buf[56];
       tmp[4] = buf[57];
       tmp[5] = buf[58];
       tmp[6] = buf[59];
       tmp[7] = buf[60];
       tmp[8] = buf[61];
       tmp[9] = buf[62];
       tmp[10] = buf[63];
    
       eth0in=atol(rip(tmp));
       printf("eth0in %s %d\n",rip(tmp),eth0in);
    
       f = fopen("/tmp/sysgrapheth0out","r");
           fgets(buf,sizeof(buf),f);
       fclose(f);
    
       tmp[0] = buf[54];
       tmp[1] = buf[55];
       tmp[2] = buf[56];
       tmp[3] = buf[57];
       tmp[4] = buf[58];
       tmp[5] = buf[59];
       tmp[6] = buf[60];
       tmp[7] = buf[61];
       tmp[8] = buf[62];
       tmp[9] = buf[63];
    
       eth0out=atol(rip(tmp));
       printf("eth0out %s %d\n",rip(tmp),eth0out);
    
       f = fopen("/tmp/sysgraphlooutout","r");
           fgets(buf,sizeof(buf),f);
       fclose(f);
    
       tmp[0] = buf[54];
       tmp[1] = buf[55];
       tmp[2] = buf[56];
       tmp[3] = buf[57];
       tmp[4] = buf[58];
       tmp[5] = buf[59];
       tmp[6] = buf[60];
       tmp[7] = buf[61];
       tmp[8] = buf[62];
       tmp[9] = buf[63];
    
       lo=atol(rip(tmp));
       printf("lo %s %d\n",rip(tmp),lo);
    
       //time
       itmp=etime;
       etime=etime-petime;
       petime=itmp;
    
       //eth0in
       itmp=eth0in;
       eth0in=eth0in-peth0in;
       peth0in=itmp;
       eth0in=eth0in/etime; //bytes per second
    
       //eth0out
       itmp=eth0out;
       eth0out=eth0out-peth0out;
       peth0out=itmp;
       eth0out=eth0out/etime; //bytes per second
    
       //lo
       itmp=lo;
       lo=lo-plo;
       plo=itmp;
       lo=lo/etime; //bytes per second
    
       printf("\n     >>>eth0 in %d bps\n     >>>eth0 out %d bps\n     >>>lo %d bps\n\n",eth0in,eth0out,lo);
    
       return 0;
    }
    Thanks,

    -Jack C
    http://www.CREPinc.com
    Last edited by crepincdotcom; 07-21-2004 at 02:20 PM. Reason: Made the question simpler

  2. #2
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    lol, along the lines of all the other thrown together peices of this code, I now have it system()'ing the problem peices to a perl script which puts it into another text file then I read that back.... there must be a better way!!

    PLEASE?

    Thanks,
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    I can't see why you don't do the whole thing in either perl or bash
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    because I don't know perl or bash

    So, basically I'm screwed? I think its the variable type, but what's big enough????
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    > lo 3045189694 2147483647 //<<<WHAT IS THAT???
    It means it's bigger than a signed integer on your system, so whatever conversion function you used returned the largest possible signed integer.

    If you want, you could use strtoul(), but that won't buy you much if you're already well into 9-digit decimals.

    Code:
       tmp[0] = buf[53];
       tmp[1] = buf[54];
       tmp[2] = buf[55];
       tmp[3] = buf[56];
       tmp[4] = buf[57];
       tmp[5] = buf[58];
       tmp[6] = buf[59];
       tmp[7] = buf[60];
       tmp[8] = buf[61];
       tmp[9] = buf[62];
       tmp[10] = buf[63];
    Two things
    1. Use strncpy, its much easier
    2. you forgot the \0 to make it a proper string
    Code:
       strncpy( tmp, &buf[53], 11 );
       tmp[11] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User crepincdotcom's Avatar
    Join Date
    Oct 2003
    Posts
    94
    Quote Originally Posted by Salem
    2. you forgot the \0 to make it a proper string
    I figured that since I defined it as 16 chars, that there would still be several nulls left on the end. I was wrong.

    Hm. Ok, well thank you, i will attempt to figure out a different way.
    -Jack C
    jack {at} crepinc.com
    http://www.crepinc.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer displaying crazy numbers issue
    By Razorblade Kiss in forum C++ Programming
    Replies: 16
    Last Post: 12-20-2004, 11:00 PM
  2. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 03:43 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21