How to read file in class?

I'm trying to read a file in a class, I do not know what I am missing. How do I call the void readfile that is in my class?
Would really appreciate some help!

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
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

class fileRead
{
  public: 
    void readFile(string filename)
    {
		// Open the input file
		ifstream in_file; 
		in_file.open(filename.c_str(), ios::in);
    }
};


int _tmain(int argc, _TCHAR* argv[])
{

string in_file;
in_file.readFile("file_name");
in_file.sort();
out_file.writeFile("file_name");


char key;
cin >> key;

return 0;
}
you should read up on classes more. THey are objects. You are calling a string object then trying to use your fileRead object..
try something like
1
2
3
4
5
int main()
{
    fileRead in;
    in.readFile( "filename);
} 


I have no idea what you are trying to do with sort? Are you trying to sort a string that is really your object you created? There also isn't even a writeFile function/class...

I also see no purpose in using a class in this case. You don't even have a constructor. Maybe try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void read( std::string );

int main()
{
    std::string file_name( "Giblit.txt" );
    read( file_name );
}

void read( std::string file_name )
{
    std::ifstream in( file_name.c_str() );
}

The read is void function so I have no idea what you are trying to accomplish also. I would just do something like
1
2
3
4
5
6
7
int main()
{
    std::string file_name( "Giblit.txt" ) , text( std::string() );
    std::ifstream in( file_name); //c++11 you can input strings
    in >> text;
    std::cout << text << std::endl;
}
Last edited on
Sry i forgot to specify what I was doing. Im trying to open up the file in the class, then sort it, then print it out.
Im going through each of the functions to make sure I caught all the errors.
Thanks to you I've been able to get the read function fixed, now I have to sort it.


Last edited on
Topic archived. No new replies allowed.