Thread: File input and Output help

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    File input and Output help

    Hello all, I am having a really tough time figuring out how to structure the input of one .dat file and outputting into another one. I have to loop through a file structured "doe, john, 1500", check for errors and then increase the mp by 50. You also have to do this through using a person class too. I think I need to use a while loop to go to eof and use getline to input correctly, but im not sure how to structure it. Any help would be fantastic, I am pretty new to C++ and I feel like I am floundering on this one.

    Data file:
    Horowitz,Jake,1200.24
    Flabitz,Bubba,1712.38
    Blackwell,Sharon,1872.93
    Molerat,Rufus,501.00
    Goodall,Jane,1567.43
    Sommerset,William,1359.57
    Van Helsing,Marcus,1675.21
    Welfirth,Bula,1903.83
    Poling,Kerri,1243.32
    Lee,Aswari Kalim,1839.42
    Lisa,Mona,2409.48
    Lisbon,Frieda,1543.70
    Hatcher,Gary,1220.13

    person class
    Code:
    #pragma once
    #if !defined(person_included)
    #define person_included
    #include <string>
    #include <iostream>
    
    
    class person{
    public:
        std::string first_name;
        std::string last_name;
        double monthly_pay;
    
    
        person():
        first_name ("john"),
        last_name ("doe"),
        monthly_pay (0)
        {}
    
    
    person(const std::string& init_fn,
          const std::string& init_ln,
          float init_mp
         ):first_name(init_fn),
           last_name(init_ln),
           monthly_pay (init_mp)
        {}
    
    
        std::string get_first_name() const {return first_name;}
        std::string get_last_name() const {return last_name;}
        float get_monthly_pay() const {return monthly_pay+50.00;}
    
    
    };
    #endif
    and then here is my main
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "person class.h"
    
    
    int main() {
    
    
    std::ifstream input("person.dat");
    
    
    if (!input) {
    std::cerr << "cannot open file" << std::endl;
    return 1;
    }
    
    
    person in;
    
    
    input >> in.last_name;
    
    
    if (input.eof()) {
    std::cerr <<"no data in file" << std::endl;
    return 2;
    }
    
    
    if (input.fail() || input.peek() != ',' || !input.ignore()) {
    std::cerr <<"last name error" << std::endl;
    return 3;
    }
    
    
    if (!getline(input, in.first_name, ',')) {
    std::cerr <<"first name error" << std::endl;
    return 4;
    }
    
    
    input >> in.monthly_pay;
    
    
    if (!input) {
    std::cerr <<"error with pay" << std::endl;
    return 5;
    }
    
    
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay << "/n"<<std::endl;
    return
    0;
    
    
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Why not just structure your program into functions
    Code:
    std::istream read ( std::istream & in , person & data_belongs_to )
    {
           if ( in )
        {
           //fix your series of if statements here
         }
       return in;
    }
    EDIT
    Actually I don't understand why you concern yourself with whether it is the end of file or some other error, for the time being, you only need to know whether the input was successful
    Last edited by Aslaville; 07-17-2013 at 01:53 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    I have the error checking in there because it is part of my assignment. If there are errors then I have to return some kind of error code. I know it makes it more messy but I have to have it in there.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    right now the program is exiting with code 1, cannot open file. How can I change that
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "person class.h"
    
    
    int main() {
    
    
    std::ifstream input("person.dat");
    
    
    if (!input) {
    std::cerr << "cannot open file" << std::endl;
    return 1;
    }
    while (!EOF){
    person in;
    
    
    input >> in.last_name;
    
    
    if (input.eof()) {
    std::cerr <<"no data in file" << std::endl;
    return 2;
    }
    
    
    if (input.fail() || input.peek() != ',' || !input.ignore()) {
    std::cerr <<"last name error" << std::endl;
    return 3;
    }
    
    
    if (!getline(input, in.first_name, ',')) {
    std::cerr <<"first name error" << std::endl;
    return 4;
    }
    
    
    input >> in.monthly_pay;
    
    
    if (!input) {
    std::cerr <<"error with pay" << std::endl;
    return 5;
    }
    
    
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay << "/n"<<std::endl;
    }
    return
    0;
    
    
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Generally, you should not use eof as a loop condition (google the why!).
    It is much better to check if a read/write failed to know when you've hit the end of a file. You can test a stream by simply using it in a boolean context, such as an if statement.
    The stream operators and getline also return the stream itself, you can chain operations:

    if (!stream) // Last operation failed
    if (!(stream >> var)) // Read operation failed
    if (!std::getline(stream, var)) // Read operation failed
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Input/output
    By JJFMJR in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2007, 07:50 AM
  2. file input/output
    By Raison in forum C Programming
    Replies: 3
    Last Post: 10-12-2003, 10:19 PM
  3. File Input / Output Help?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-18-2002, 10:20 AM
  4. file input/output
    By Sean Gilbride in forum C Programming
    Replies: 3
    Last Post: 02-10-2002, 09:59 PM
  5. File Input-output
    By ucme_me in forum C Programming
    Replies: 6
    Last Post: 12-14-2001, 02:27 PM

Tags for this Thread