read/write file contents

Discussion in 'C++' started by blip, Aug 10, 2005.

  1. blip

    blip Guest

    Is this acceptable ? It seems too easy and too simple...

    #include<iostream>
    #include<fstream>
    #include<cstdlib>
    #include<string>

    struct Person{
    char name[25] ;
    int age ;
    char ssn[9] ;
    } person[5] ;



    int main(int argc, char *argv[])
    {
    std::ifstream input ;
    std::eek:fstream output ;
    std::string name ;
    int age ;

    input.open("file.txt", std::ios::in) ;
    output.open("fileout.txt", std::ios::eek:ut) ;

    if(!input){
    std::cout << "File not found" << std::endl ;
    exit(1) ;
    }

    for(int i = 0; i < 5; i++)
    input >> person.name >> person.age >> person.ssn ;

    for(int k = 0; k < 5; k++)
    std::cout << person[k].name << " " << person[k].age << " " <<
    person[k].ssn << std::endl ;

    // write some text to a file
    name = "Me" ;
    age = 105 ;

    output << name << " " << age << std::endl ;





    return 0 ;
    }


    Here's the file contents exactly as in file.txt

    bob
    44
    123-23-4532
    fred
    61
    434-98-7654
    hilbert
    23
    772-81-7108
    peter
    12
    132-64-0132
    jake
    51
    565-61-0909


    I'd like to know what you people think of this .

    Regards,
    Me
    blip, Aug 10, 2005
    #1
    1. Advertising

  2. blip

    John Ratliff Guest

    blip wrote:
    > Is this acceptable ? It seems too easy and too simple...
    >
    > ... snip ...
    >
    >
    > Here's the file contents exactly as in file.txt
    >
    > bob
    > 44
    > 123-23-4532
    > fred
    > 61
    > 434-98-7654
    > hilbert
    > 23
    > 772-81-7108
    > peter
    > 12
    > 132-64-0132
    > jake
    > 51
    > 565-61-0909
    >
    >
    > I'd like to know what you people think of this .
    >
    > Regards,
    > Me
    >



    It looks okay to me. But I didn't test it. If it works when you run it,
    then I'd say it's probably good. Maybe someone sees something wrong I
    don't. I only glanced for a minute.

    --John Ratliff
    John Ratliff, Aug 10, 2005
    #2
    1. Advertising

  3. In message <kymKe.13373$084.5880@attbi_s22>, John Ratliff
    <> writes
    >blip wrote:
    >> Is this acceptable ? It seems too easy and too simple...
    >> ... snip ...
    >> Here's the file contents exactly as in file.txt
    >> bob
    >> 44
    >> 123-23-4532
    >> fred
    >> 61
    >> 434-98-7654
    >> hilbert
    >> 23
    >> 772-81-7108
    >> peter
    >> 12
    >> 132-64-0132
    >> jake
    >> 51
    >> 565-61-0909
    >> I'd like to know what you people think of this .
    >> Regards,
    >> Me
    >>

    >
    >
    >It looks okay to me. But I didn't test it. If it works when you run it,
    >then I'd say it's probably good. Maybe someone sees something wrong I
    >don't. I only glanced for a minute.


    As an example of file reading and writing it's OK up to a point, but it
    won't work if any of the strings contain spaces. operator>>(istream&,
    string&) stops when it reaches whitespace.

    For robust code it's probably better to define overloaded operators >>
    and << for Person, which validate the data and provide some kind of
    quoting convention to deal with the problem of embedded spaces etc.

    (And it really ought to test for errors on all the file operations.)

    As an example of good program design it has flaws in other areas, but I
    don't think that was the point of the question.

    --
    Richard Herring
    Richard Herring, Aug 10, 2005
    #3
    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. Kamarulnizam Rahim
    Replies:
    4
    Views:
    203
    Robert Klemme
    Jan 28, 2011
  2. JL
    Replies:
    2
    Views:
    86
    Tim Chase
    Dec 14, 2013
  3. Cameron Simpson
    Replies:
    0
    Views:
    78
    Cameron Simpson
    Dec 15, 2013
  4. Chris Angelico
    Replies:
    0
    Views:
    86
    Chris Angelico
    Dec 15, 2013
  5. Tim Chase
    Replies:
    0
    Views:
    85
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page