OPening a file with a class

HI Guys,
I have been having lots of issues not understanding why this isn't working.
I'm trying to
1. Read the contents of the file into the string object
2. Add a member function contents() to return the string so that you can display it.

When I compile I get the error below...
TMA2-3.cpp:63:20: error: expected primary-expression before 'tc'
cout << textClass tc("File_TMA2-3.txt").contents;


I'm not even sure if that's the way it should be done. Isn't "tc" just referring to the instantiation of the class object? Any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
#include <iostream>
#include <fstream>
using namespace std;



class textClass{
	
	public:
		textClass(){		//default constructor
		cout << "default constructor";
		}
		
		textClass(string x) { //constructor with parameter
		
			string line;
			ifstream f(x);
			cout << x << endl;  // just for visual aid. 
			
			f.open(x);			
			if (f.is_open()){
cout << "file opened";				
			}else{
				cout << "file not opened";			
			}				
		}
		
		contents() {
					
		}
		
	};

int main() {
		
	cout << textClass tc("File_TMA2-3.txt).contents;
			
} 
Either:
1
2
textClass tc("File_TMA2-3.txt) ; // create an object 'tc'
std::cout << tc.contents() ; // call member function on the object  

Or:
std::cout << textClass("File_TMA2-3.txt).contents() ; // use the anonymous object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>
#include <fstream>

struct text_class {

    text_class() = default ;

    explicit text_class( const std::string& file_name ) {

        if( std::ifstream file{file_name} ) { // if file was opened successfully

            // read each character in the file and append to text
            char c ;
            while( file.get(c) ) text += c ;
        }

        else {

            // failed to open file
        }
    }

    const std::string& contents() const { return text ; }

    private: std::string text ;
};

int main() {

    const std::string this_file = __FILE__ ;

    std::cout << text_class(this_file).contents() ;
}
Changes made but still not getting opened file as expected.


class textClass{

public:
textClass(){ //default constructor
cout << "default constructor";
}

textClass(string fileName){ //constructor with parameter

cout << fileName << endl; <----- THis outputs File_TMA2-3.txt as expected
ifstream f(fileName); <-- is this not the correct way ??

if (f.is_open())
cout << "File is open";
else
cout << "File is not open"; <-- Always get file not opened...


}

};


int main() {

textClass tc("File_TMA2-3.txt");
//cout << tc.contents() << endl;


}
> ifstream f(fileName); <-- is this not the correct way ??

It is the correct way.


> Always get file not opened...

Make sure that the file File_TMA2-3.txt is in the current working directory.

To see what the current working directory is, add this code to main:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <unistd.h> // for ::getcwd()

int main()
{
    char buff[1024] ;
    std::cout << "current working directory: "
              << ::getcwd( buff, sizeof(buff) ) << '\n' ;
              
    // ...          
}

OK almost there.

1. ifstream f(filename) should be ifstream(filename.c_str())
2. The file is very much in the cwd.

I'm getting an error regarding the contents function...I'm not sure why when I'm not trying to convert it to an int.

1
2
3
TMA2-3.cpp: In member function 'int textClass::contents()':
TMA2-3.cpp:29:11: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'int' in return
    return text;



Thoughts?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
#include <fstream>
#include <string>
using namespace std;



class textClass{
	
	public:
		textClass(){cout << "default constructor" << endl;} //default constructor		
		string line, text;
		
		textClass(string fileName){
			
			fstream f(fileName.c_str());						
			while (getline(f,line))
				text += line + '\n';
				
		};
		
		contents(){	
			return text;
		};
				
		
};
	
	
int main() {
	
	textClass tc("File_TMA2-3.txt");
	cout << tc.contents() << endl;
	
			
}

Last edited on
If you require .c_str() when opening a filestream then you need to upgrade your compiler (or turn c++11 features on).

You haven't declared the return type of contents(). Don't you think that you ought to before the compiler makes an incorrect assumption? Presumably it is
string contents() {
Topic archived. No new replies allowed.