Thread: File Input and Output with error checking

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

    File Input and Output with error checking

    I am a beginner C++ programmer and I am having a lot of difficulty with this assignment for my class. I am inputing file data from person.dat and outputting to output.dat. I have to have error checking throughout, I am getting errors and I am not sure how to get around the problem. Any help would be great
    person.dat file contents:

    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
    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.00)
        {}
    
    
    person(const std::string& init_fn,
          const std::string& init_ln,
          double 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;}
        double get_monthly_pay() const {return monthly_pay+50.00;}
    
    
        /*friend std::ostream& operator<<(
            std::ostream& j,
            const person& s)
            {
            j << s.last_name <<","<< s.first_name<<","<< s.monthly_pay<<"/n";
            return j;}*/
    };
    #endif
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "person class.h"
    
    
    int main() {
    
    
    std::ifstream input("person.dat");
    person in;
    
    
    if (!input) {
    std::cerr << "cannot open file" << std::endl;
    return 6;
    }
    
    
    for (;;){
    try{
    if (input.eof()) {throw 1;}
    
    
    if (!std::getline(input, in.last_name, ',')) {throw 2;}
    
    
    
    
    if (!std::getline(input, in.first_name, ',')) {throw 3;}
    
    
    
    
    input >> in.monthly_pay;
    
    
    if (!input) {throw 4;}
    
    
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    
    
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    }
    return
    0;
    
    
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I thought you had started a similar thread only to disappear and leave others going over your work File input and Output help.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    You're right, I did have a similar thread but something came up and I was unable to access the thread for a while. Since then I changed the code some, and am having different problems.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    What errors are you getting as you say in the post #1?

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    I made a slight change to the code, but right now it is exiting with code 0. The output file is only containing one line (the last line of person.dat) and it isn't getting the 50 dollar increase as placed in person class.h.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "person class.h"
    
    
    int main() {
    
    
    std::ifstream input("person.dat");
    person in;
    
    
    if (!input) {
    std::cerr << "cannot open file" << std::endl;
    return 6;
    }
    
    
    for (;;){
    try{
    if (input.eof()) {break;}
    
    
    if (!std::getline(input, in.last_name, ',')) {throw 2;}
    
    
    
    
    if (!std::getline(input, in.first_name, ',')) {throw 3;}
    
    
    
    
    input >> in.monthly_pay;
    
    
    if (!input) {throw 4;}
    
    
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    
    
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    }
    return 0;
    
    
    }

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why are you checking the eof before any read operation occurred? eof flag is set by read operation when EOF is encountered. After just opening file it will be false even if the file is empty.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You see you are opening the file every time you loop and every time you open the contents get overwritten.
    Code:
    for (;;){
    try{
    //code ...
    
    
    std::ofstream stream ("output.dat");
    //code ...
    return 0;
    
    
    }
    The file should be opened outside the loop so that you use the same file descriptor throughout the program to ensure the correct offset with each loop

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    so something like this?
    Code:
    if (!input) {throw 4;}
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    
    }
    return 0;

  9. #9
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Magi View Post
    so something like this?
    Code:
    if (!input) {throw 4;}
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    
    }
    return 0;
    Make sure you open the output file outside the loop else you create files with similar names in the same directory and they get overwritten and what you get is the file created during the last loop.
    The same way you open the input file before entering the loop
    Last edited by Aslaville; 07-22-2013 at 01:23 PM.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    When I do this it just prints out the last first name and monthly pay from the person.dat file.
    Code:
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    }
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    return 0;
    
    
    }

  11. #11
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Magi View Post
    When I do this it just prints out the last first name and monthly pay from the person.dat file.
    Code:
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    }
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
    return 0;
    
    
    }
    Am talking of this
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "person class.h"
     
     
    int main() {
     
     
    std::ifstream input("person.dat");
    std :: ostream output ("output.dat")
    person in;
     
     
    if (!input) {
    std::cerr << "cannot open file" << std::endl;
    return 6;
    }
     
     
    for (;;){//<-- this is a loop
    try{
    if (input.eof()) {break;}
     
     
    if (!std::getline(input, in.last_name, ',')) {throw 2;}
     
     
     
     
    if (!std::getline(input, in.first_name, ',')) {throw 3;}
     
     
     
     
    input >> in.monthly_pay;
     
     
    if (!input) {throw 4;}
     
     
    std::ofstream stream ("output.dat");
    stream << in.last_name <<"," << in.first_name << "," << in.monthly_pay<<std::endl;
     
     
    }catch(int error){
    std::cerr <<"an error occured" << error <<std::endl;
    }
    }
    return 0;
     
     
    }
    Last edited by Aslaville; 07-22-2013 at 01:35 PM.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    That's absolutely fantastic thank you, one final question. My person class return of monthly pay isnt properly increasing the pay by 50.00 dollars, what can I do to fix that?

  13. #13
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Code:
    double get_monthly_pay() const {return monthly_pay+50.00; }
    Try getting rid of the const keyword??!

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    It's still not increasing the pay.

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Never mind, I fixed it. Thank you so much for all your help. Sorry I disappeared from that first thread, I had a small crisis at home. I really appreciate the info, I learned a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with error checking on input from user.
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 01-23-2009, 12:59 PM
  2. error checking file
    By Nexus-ZERO in forum C Programming
    Replies: 11
    Last Post: 01-29-2008, 04:02 AM
  3. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  4. Writing to a file checking if the input is new
    By BianConiglio in forum C Programming
    Replies: 3
    Last Post: 07-18-2005, 05:59 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM

Tags for this Thread