get values from data file to integer variables..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jm0

    #1

    get values from data file to integer variables..

    Hi, im developing this program, and then i ran into some trouble "oooh
    no!" hate to ask.. have search google and this page for something to
    spilt things up in arrays or whatever can be used, spent my half day
    trying to find a solution, but nothing :( then i agreed with my self
    that i have to ask. So her's the question/problem.

    Link to project:


    I have a data file called "data", it contains:

    timer: 1928384938485
    total: 119283
    drink: 10.5

    Then in my sourcecode i've made a piece of code that reads the "data"
    file and prints it out through a string. Now what i want this program
    to do, is to take the value of fx.. "timer: " which is "1928384938 485"
    into a integer so i can do something with it :) Can someone suggets how
    i can do this..?

    btw. my english i not so good since im from denmark :D

    Thanks for help, if something ;)

  • TB

    #2
    Re: get values from data file to integer variables..

    jm0 sade:[color=blue]
    > Hi, im developing this program, and then i ran into some trouble "oooh
    > no!" hate to ask.. have search google and this page for something to
    > spilt things up in arrays or whatever can be used, spent my half day
    > trying to find a solution, but nothing :( then i agreed with my self
    > that i have to ask. So her's the question/problem.
    >
    > Link to project:
    > http://kawsper.dyndns.dk/~jmo/develo...jects/n0rdnat/
    >
    > I have a data file called "data", it contains:
    >
    > timer: 1928384938485
    > total: 119283
    > drink: 10.5
    >
    > Then in my sourcecode i've made a piece of code that reads the "data"
    > file and prints it out through a string. Now what i want this program
    > to do, is to take the value of fx.. "timer: " which is "1928384938 485"
    > into a integer so i can do something with it :) Can someone suggets how
    > i can do this..?[/color]

    It's basic math:

    "1928384938 485"

    5 * 1 +
    8 * 10 +
    4 * 100 +
    8 * 1000 +
    3 * 10000 +
    9 * 100000 +
    4 * 1000000 +
    8 * 10000000 +
    3 * 100000000 +
    8 * 1000000000 +
    2 * 10000000000 +
    9 * 100000000000 +
    1 * 1000000000000 = 1928384938485

    Of course there are function like 'strtol' that can do it for you,
    but with a large number like that you might want to worry about
    overflow and alternative datatypes for representation, unless you're
    on a 64 bit system.

    --
    TB @ SWEDEN

    Comment

    • phish.baccaruda@gmail.com

      #3
      Re: get values from data file to integer variables..

      hey jm0,


      Id use this -

      ......

      int timer;

      ifstream myfile ("data1");

      if (myfile.is_open ())
      {

      // Skip over the text and up to the number.
      File.seekg(6);


      // Read up to the whitespace or eol.
      myfile >> timer;

      // Reset the index back to the beginning of the file.
      File.seekg(ios: :beg);

      ......

      if your intrested theres some cool I/O refrence tutorials at :-





      Comment

      • Jim Langston

        #4
        Re: get values from data file to integer variables..


        "jm0" <jesper.mick.ol sen@gmail.com> wrote in message
        news:1139076731 .965152.253360@ f14g2000cwb.goo glegroups.com.. .[color=blue]
        > Hi, im developing this program, and then i ran into some trouble "oooh
        > no!" hate to ask.. have search google and this page for something to
        > spilt things up in arrays or whatever can be used, spent my half day
        > trying to find a solution, but nothing :( then i agreed with my self
        > that i have to ask. So her's the question/problem.
        >
        > Link to project:
        > http://kawsper.dyndns.dk/~jmo/develo...jects/n0rdnat/
        >
        > I have a data file called "data", it contains:
        >
        > timer: 1928384938485
        > total: 119283
        > drink: 10.5
        >
        > Then in my sourcecode i've made a piece of code that reads the "data"
        > file and prints it out through a string. Now what i want this program
        > to do, is to take the value of fx.. "timer: " which is "1928384938 485"
        > into a integer so i can do something with it :) Can someone suggets how
        > i can do this..?
        >
        > btw. my english i not so good since im from denmark :D
        >
        > Thanks for help, if something ;)[/color]

        Normally, this would be rather trivial. Open ifstream, and then >> into the
        int. The problem is, a 4 byte int's max value is:
        4294967295
        your timer value is:
        1928384938485

        this is quite a bit bigger than an int.

        You're gonna need a 32 byte int to hold this. Does your system support one?
        Otherwise it would just be:

        unsigned int Timer;
        unsigned int Total;
        float Drink;
        std::ifstream DataFile("data" );
        if (DataFile.is_op en())
        {
        std::string KeyValue;
        if ( DataFile >> KeyValue )
        {
        if ( KeyValue == "timer:" )
        if ( DataFile >> Timer )
        // success
        else
        // couldn't read value
        else
        // timer: expected, not found
        }


        }
        DataFile.close( );

        I close datafile even if it isn't open, and not only that but it is not even
        strictly required to .close() datafile as when DataFile goes out of scope
        the destructor will close it. It is just way I do it.

        But, as stated, this won't quite work for you since your value in Timer
        won't fit in a 16 bit unsigned int anyway.

        Maybe you can load it into a std::string or something? What do you need to
        do with it?



        Comment

        • Luke Meyers

          #5
          Re: get values from data file to integer variables..

          Jim Langston wrote:[color=blue]
          > Normally, this would be rather trivial. Open ifstream, and then >> into the
          > int. The problem is, a 4 byte int's max value is:
          > 4294967295
          > your timer value is:
          > 1928384938485
          >
          > this is quite a bit bigger than an int.
          >
          > You're gonna need a 32 byte int to hold this. Does your system support one?[/color]

          Beg pardon? log(19283849384 85) < 41. So it needs 64 bits* (8 bytes),
          not 256 bits (32 bytes).

          Do I recall the following items correctly?
          * sizeof(int) can be as small as 16
          * sizeof(long) can be as small as 32
          * long long is non-standard (or was it incorporated in '03?)

          Luke

          * Yes, technically only 41 bits are required, but I rarely acknowledge
          the existence of numbers that are not powers of 2.

          Comment

          Working...