reading files

Discussion in 'C++' started by Harry Overs, Aug 26, 2004.

  1. Harry Overs

    Harry Overs Guest

    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,
    Harry Overs, Aug 26, 2004
    #1
    1. Advertising

  2. Harry Overs wrote:
    > 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,
    >
    >

    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:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.comeaucomputing.com/learn/faq/
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 26, 2004
    #2
    1. Advertising

  3. Harry Overs wrote:

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


    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.
    Jacek Dziedzic, Aug 26, 2004
    #3
  4. Harry Overs

    Mike Wahler Guest

    "Harry Overs" <> wrote in message
    news:cgkpdt$2au$...
    > 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


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

    > or should I
    > just read it all in as char* and then convert to the correct format?


    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
    Mike Wahler, Aug 26, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rob hadow

    JAR files reading list files

    rob hadow, May 21, 2004, in forum: Java
    Replies:
    4
    Views:
    4,774
    rob hadow
    May 21, 2004
  2. crazyprakash
    Replies:
    4
    Views:
    3,370
    adrian
    Oct 30, 2005
  3. Replies:
    4
    Views:
    950
    M.E.Farmer
    Feb 13, 2005
  4. Replies:
    0
    Views:
    779
  5. Narendra Sisodiya

    Reading files inside zip files from Javascript

    Narendra Sisodiya, Dec 7, 2009, in forum: Javascript
    Replies:
    7
    Views:
    279
    Michael Haufe (\TNO\)
    Dec 8, 2009
Loading...

Share This Page