reading files

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

    #1

    reading files

    I'm using the ifstream to read the contents of a file using the >>
    operators.

    However the problem that I have is that the file is made up of double and
    string variables. Is there an easy way to read both types in or should I
    just read it all in as char* and then convert to the correct format?

    cheers,


  • Thomas Matthews

    #2
    Re: reading files

    Harry Overs wrote:[color=blue]
    > I'm using the ifstream to read the contents of a file using the >>
    > operators.
    >
    > However the problem that I have is that the file is made up of double and
    > string variables. Is there an easy way to read both types in or should I
    > just read it all in as char* and then convert to the correct format?
    >
    > cheers,
    >
    >[/color]
    If you know when to read in the doubles, then read in using
    strings and doubles.

    Although you may find that using std::string will be easier
    than C style strings.

    --
    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

    • Jacek Dziedzic

      #3
      Re: reading files

      Harry Overs wrote:
      [color=blue]
      > I'm using the ifstream to read the contents of a file using the >>
      > operators.
      >
      > However the problem that I have is that the file is made up of double and
      > string variables. Is there an easy way to read both types in or should I
      > just read it all in as char* and then convert to the correct format?[/color]

      If you know which fields are double and which are string, than yes,
      you could use the ">>" operator to easily read them:

      myfile >> mydouble >> mydouble >> mystring >> ...

      The trouble with reading strings via ">>" is, however, that
      if you have spaces embedded in your strings they will split the
      input by words. In that case you might want to look at the
      get(...) and getline(...) functions.

      HTH,
      - J.

      Comment

      • Mike Wahler

        #4
        Re: reading files


        "Harry Overs" <hovers@dera.go v.uk> wrote in message
        news:cgkpdt$2au $1@hamble.qinet iq.com...[color=blue]
        > I'm using the ifstream to read the contents of a file using the >>
        > operators.
        >
        > However the problem that I have is that the file is made up of double and
        > string variables. Is there an easy way to read both types in[/color]

        Yes, but you'll need to know in advance the exact format of the
        data, and depend upon it being correct or make sure bad data is
        handled properly.

        std::ifstream in("filename") ;
        double d;
        std::string s;

        in >> d >> s; // read a double, then a string

        But if the characters making up the 'double' value are invalid,
        'in >> d' will fail, leaving those characters in the stream,
        and setting the stream to a 'fail' state, in which it will stay
        until you specifically reset it. Another problem is that if
        any of your strings contain whitespace, 'in >> s' will stop
        reading at the first one. E.g input of "John Doe" will cause
        extraction of only "John".
        [color=blue]
        > or should I
        > just read it all in as char* and then convert to the correct format?[/color]

        Almost. Change 'char*' to 'std::string', and I agree.


        IMO this is the best method to 'default' to. You get better control.
        A function such as 'strtod()' can do the numeric conversions and has good
        error reporting.

        while stream state == good
        Read a line.
        Convert (if numeric) and store.
        go to 1

        if stream state != eof
        read error

        -Mike


        Comment

        Working...