How to open a filename which is in one class & then use that in a different class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deenar
    New Member
    • Sep 2010
    • 13

    How to open a filename which is in one class & then use that in a different class

    Hi, I am new to C++ coding and needed some help.I have written some c++ code in codeblocks but I am getting errors when i am trying to compile it. I have a file named files.txt and this file consists of the names of the files that actually contain the data that I need to present. I am trying to get the name of files from from once class and passing it through to another to process. These are the codes that i have:
    Code:
    main.cpp
    #include <iostream>
    #include <fstream>
    #include <string>
    #include "Files.h"
    #include "Datafile.h"
    
    using namespace std;
    
    int main()
    {
        ifstream infile("fileListAug.txt");
        if(!infile) return -1;
    
        Files F;
        infile >> F;
    
        ofstream ofile("2011-Aug.csv");
    
        ofile << F;
    
        return 0;
    }
    Code:
    Files.cpp
    #include "Files.h"
    
    Files::Files()
    {
    
    }
    
    Files::Files(const string m_filenames)
    
    {
        m_filename = m_filenames;
    
    }
    
    istream & operator >> (istream & input, Files & F)
    {
        do{
        getline(input, F.m_filename, ';');
        cout << F.m_filename << endl;
    
        }while(! input.eof());
        return input;
    }
    Code:
    Datafile.cpp
    #include "Datafile.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    Datafile::Datafile()
    {
    
    }
    
    Datafile::Datafile(const string m_lines)
    
    {
        m_line = m_lines;
    
    }
    
    void Datafile::GetDatafile()
    {
        ifstream infile;
        infile.open(files.m_filename());//(*******Error points to this  line)
        
    Datafile L;
        infile >> L;
    
        ofstream ofile("2011-Aug.csv");
    
        ofile << L;
    }
    
    istream & operator >> (istream & input, Datafile & L)
    {
        do{
        getline(input, L.m_line, ';');
    
        }while(! input.eof());
    
        return input;
    }
    
    
    ostream & operator << (ostream & os, const Datafile & L)
    {
        cout << "!" << L.files << "!" << endl;
        //os << L.lines << '\n';
        return os;
    }
    The error message that i get is error: no match for call to (std:: string). For ther line with the error I have used (*******Error points to this line).

    Thanks so much...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    infile.open(files.m_filename());//(*******Error points to this  line)
    The istream open requires a C_style string: char array + niull terminator. Your filename is a C++ string object.

    Just ask the filename object to present itself as a C string:

    Code:
    infile.open(files.m_filename.c_str());
    There are other issues but try tis first.
    Last edited by weaknessforcats; Apr 20 '12, 01:53 AM.

    Comment

    • deenar
      New Member
      • Sep 2010
      • 13

      #3
      Thanks weaknessforcats , it got rid of the error msg. You mentioned that there are other issues, please advice as to what is wrong. Thanks

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        Files::Files(const string m_filenames)
         10. 
         11. {
         12.     m_filename = m_filenames;
         13. 
         14. }
        This code assigns the m_filename of the function argument to itself. The object is unchanged. The problem is the member variable and the function argument have the same name.

        Use the this pointer to identify member variables and functions. A m_xxxx naming convention is pointless:
        Code:
        Files::Files(const string m_filenames)
          
          {
              this->m_filename = m_filenames;
          
          }
        You have couts all over. Take them all out. Learn to se your debugger. Ideally all displays are in main(), or better, managed by an object whose responsibility is the screen. Very irritating to have odd displays popping out all over.

        Try a front-end/back-end apporach where the back_end does all the work and the front-end is only the interface between the user and the back-end.

        You have no copy cnstructors or assignment operators or destructors. The rule is if you have to write either a ctor or a dtor or and assignment operator, then you have to write all three. This rule is based on not being able to use one of the compiler's version of these functions. That being the case, prefer to use no compiler versions of these functions.

        It also looks like some of your classes (notably Files) have public data members. Big no-no. No public data members whatsoever should be allowed. Having them breaks encapsulation and data hiding which are two of the main reasons to have a class in the first place.

        You have hard-coded literals and constants. That means you have to rebuild if you need to change any of these.

        Be consistent in the use of references. The rule is that if the function does not change the value of the argument, then the argument should be a const reference. Not using references forces a copy of the argument when it isn't needed.
        Last edited by weaknessforcats; Apr 20 '12, 05:36 AM. Reason: forgot a code tag

        Comment

        • deenar
          New Member
          • Sep 2010
          • 13

          #5
          Thank-you so much, I really appreciate it.

          Comment

          Working...