File handling problem

This is a discussion on File handling problem within the C++ Programming forums, part of the General Programming Boards category; HI folks, This is my code. How can i pass a outside structure parameter to a class method? Code: using ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53

    File handling problem

    HI folks,
    This is my code. How can i pass a outside structure parameter to a class method?
    Code:
    using namespace std;
    struct status 
    {
        char name[80];
        double balance;
        unsigned long account_num;
    };
    
    
    class FileHandler
    {
    public:
        FileHandler(string);
        ~FileHandler();
        
        int getRecords(struct status *);
        int putRecords(struct status *);
      
        
    
    private:
        string fileName;
        fstream fPointer; // ref
    };
    
    
    FileHandler :: FileHandler(string fname)
    {
        fPointer.open(fname, ios::out | ios::binary);
    }
    int FileHandler :: getRecords(struct status *acc)
    {
        strcpy(acc.name, "Heartly");
        acc.balance = 1000;
        acc.account_num = 3;
        return 0;
    }
    int FileHandler :: putRecords(struct status *acc)
    {
        cout << acc.name << endl;
        cout << "Account # " << acc.account_num;
        cout.precision(2);
        cout.setf(ios::fixed);
        cout << endl << "Balance: $" << acc.balance<<endl;
        return 0;
    }

    In FileHandler constructor i got the below error
    clshandle.cpp: In constructor ‘FileHandler::FileHandler(std::string)’:
    clshandle.cpp:31:45: error: no matching function for call to ‘std::basic_fstream<char>:: open(std::string&, std::_Ios_Openmode)’
    /usr/include/c++/4.5/fstream:866:7: note: candidate is: void std::basic_fstream<_CharT, _Traits>:: open(const char*, std::ios_base:: openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base:: openmode = std::_Ios_Openmode]

    in getReords() methods, i got the below error.
    clshandle.cpp: In member function ‘int FileHandler::getRecords(status*)’:
    clshandle.cpp:53:13: error: request for member ‘name’ in ‘acc’, which is of non-class type ‘status*’
    clshandle.cpp:54:6: error: request for member ‘balance’ in ‘acc’, which is of non-class type ‘status*’
    clshandle.cpp:55:6: error: request for member ‘account_num’ in ‘acc’, which is of non-class type ‘status*’
    clshandle.cpp: In member function ‘int FileHandler:: putRecords(status*)’:
    clshandle.cpp:60:14: error: request for member ‘name’ in ‘acc’, which is of non-class type ‘status*’
    clshandle.cpp:61:30: error: request for member ‘account_num’ in ‘acc’, which is of non-class type ‘status*’
    clshandle.cpp:64:38: error: request for member ‘balance’ in ‘acc’, which is of non-class type ‘status*’

    Last edited by infantheartlyje; 10-25-2011 at 07:04 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    #1. Your FileHandler constructor should be:
    Code:
    FileHandler :: FileHandler(const string& fname)
    {
        fPointer.open(fname.c_str(), ios::out | ios::binary);
    }
    The error points to the fact that there is no match for a string argument in the stream's open call. Basically, the older version of the standard did not support using a std::string as a file name parameter. The newer one supposedly does allow this. So, to get things to work you have to convert the std::string to something the open call can use... such as a const char* type which is what the c_str member function does.

    #2. Your other error is because in your code acc is a pointer to a struct but your are attempting to access it as if it were not a pointer. You would need to use -> instead of '.' to access the struct's data members. However, I'd just use a reference instead of a pointer.

    #3. The putRecords member function should be declared const as it does not modify the object.

    #4. Is there a reason the status struct's name member is not a std::string?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling problem, please help
    By alice06 in forum C Programming
    Replies: 1
    Last Post: 04-07-2011, 10:06 PM
  2. File Handling Problem
    By amit_200 in forum C Programming
    Replies: 1
    Last Post: 12-05-2007, 12:09 AM
  3. another file handling problem
    By 1978Corvette in forum C Programming
    Replies: 5
    Last Post: 04-27-2006, 12:38 AM
  4. Problem in file handling.
    By Abhid in forum C Programming
    Replies: 6
    Last Post: 02-18-2005, 11:11 PM
  5. problem with file handling
    By gell10 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2003, 02:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21