File to Structure?

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

    #1

    File to Structure?

    How do i go from a file containing data, like ...

    MJ914
    Porsche
    914-6
    Irish_Green
    2
    2700
    30000
    40000

    TO

    struct car
    {
    char rego[6];
    char make[15];
    char model[10];
    char colour[15];
    int cap[4];
    int doors[1];
    int costp[6];
    int sellp[6];
    };

  • TheShadow1

    #2
    Re: File to Structure?

    struct car
    {
    char rego[6];
    char make[15];
    char model[10];
    char colour[15];
    int cap[4];
    int doors[1];
    int costp[6];
    int sellp[6];
    };


    void main()

    ifstream fin;

    for(x = 0; x++; x > 11 || x = EOF;)
    ifstream fin("cars.dat") ;
    fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >>
    car.doors >> car.costp >> car.sellp >>endl;

    Comment

    • TheShadow1

      #3
      Re: File to Structure?

      I dont think thats the way to do it anyway ...

      i think need to use getline();

      Comment

      • Owen Jacobson

        #4
        Re: File to Structure?

        On Wed, 13 Oct 2004 21:52:07 -0400, TheShadow1 wrote:
        [color=blue]
        > How do i go from a file containing data, like ...
        >
        > MJ914
        > Porsche
        > 914-6
        > Irish_Green
        > 2 // <--
        > 2700 // <--
        > 30000 // <--
        > 40000 // <--
        >
        > TO
        >
        > struct car
        > {
        > char rego[6];
        > char make[15];
        > char model[10];
        > char colour[15];[/color]

        These might be better off as std::string values, as defined in the
        standard <string> header.
        [color=blue]
        > int cap[4]; // <--
        > int doors[1]; // <--
        > int costp[6]; // <--
        > int sellp[6]; // <--
        > };[/color]

        The corresponding fields in your input data don't look like integer
        *arrays* to me...

        --
        Some say the Wired doesn't have political borders like the real world,
        but there are far too many nonsense-spouting anarchists or idiots who
        think that pranks are a revolution.

        Comment

        • Malte Starostik

          #5
          Re: File to Structure?

          TheShadow1 schrieb:
          [color=blue]
          > struct car
          > {
          > char rego[6];
          > char make[15];
          > char model[10];
          > char colour[15];[/color]

          Those are dangerous and cannot be used for streaming as used below, use
          std::string instead of char arrays.
          [color=blue]
          > int cap[4];[/color]

          This declares an array of four integers where you presumably wanted an
          integral type capable of storing values with up to four decimal digits.
          [color=blue]
          > int doors[1];
          > int costp[6];
          > int sellp[6];[/color]

          Same for those.
          [color=blue]
          > };
          >
          >
          > void main()[/color]

          main() always returns int (and its body needs enclosing braces like any
          other function).
          [color=blue]
          >
          > ifstream fin;
          >
          > for(x = 0; x++; x > 11 || x = EOF;)[/color]

          You never declared x nor EOF, there's one ; too much in the for
          statement and the expressions' order is most probably wrong.
          [color=blue]
          > ifstream fin("cars.dat") ;
          > fin >> car.rego >> car.make >> car.model >> car.colour >> car.cap >>
          > car.doors >> car.costp >> car.sellp >>endl;[/color]

          Except for missing error checking, that should work after dropping the[color=blue][color=green]
          >>endl part and fixing the above issues.[/color][/color]

          HTH,
          Regards,
          Malte

          Comment

          • TheShadow1

            #6
            Re: File to Structure?

            So something more like ...

            struct Car
            {
            string rego;
            string make;
            string model;
            string colour;
            int cap;
            int doors;
            int costp;
            int sellp;
            }

            Comment

            • John Harrison

              #7
              Re: File to Structure?


              "TheShadow1 " <trashedone@gma il.com> wrote in message
              news:1bff6daf25 efd3f8553337114 419eb60@localho st.talkaboutpro gramming.com...[color=blue]
              > So something more like ...
              >
              > struct Car
              > {
              > string rego;
              > string make;
              > string model;
              > string colour;
              > int cap;
              > int doors;
              > int costp;
              > int sellp;
              > }[/color]


              Yes, looks a lot better.


              Comment

              • Thomas Matthews

                #8
                Re: File to Structure?

                TheShadow1 wrote:[color=blue]
                > So something more like ...
                >
                > struct Car
                > {
                > string rego;
                > string make;
                > string model;
                > string colour;
                > int cap;
                > int doors;
                > int costp;
                > int sellp;
                > }
                >[/color]

                /* Data file from previous article:
                MJ914
                Porsche
                914-6
                Irish_Green
                2
                2700
                30000
                40000
                */

                struct Car
                {
                // Same as above
                friend istream& operator>>(istr eam& input, Car& car_);
                };

                istream& operator>>(istr eam& input, Car& new_car)
                {
                getline(input, rego);
                getline(input, make);
                getline(input, model);
                getline(input, colour);
                input >> cap >> doors >> costp >> sellp;
                return input;
                }

                Now to use this:
                int main(void)
                {
                ifstream data_file("data _file.txt");
                Car my_car;

                // Input car from data file:
                data_file >> my_car;

                return EXIT_SUCCESS;
                }

                I suggest you also overload operator<< so you can
                display the contents of a Car.

                --
                Thomas Matthews

                C++ newsgroup welcome message:

                C++ Faq: http://www.parashift.com/c++-faq-lite
                C Faq: http://www.eskimo.com/~scs/c-faq/top.html
                alt.comp.lang.l earn.c-c++ faq:

                Other sites:
                http://www.josuttis.com -- C++ STL Library book

                Comment

                Working...