Advertisement

Opening a file using a char array as the filename

Started by March 27, 2001 06:08 PM
4 comments, last by CHollman82 23 years, 10 months ago
This is what I want to do: ================================================================= class someclass { public: someclass(char _filename[]){strcopy(filename,_filename);} char filename[20]; }; int main() { someclass cube("cube.dat") ifstream.getdata; getdata.open(cube.filename); } ================================================================= the getdata.open() call is the problem. Anyone know how to do this?
codeXtremeif(you->intelligence < radish->intelligence) you->STFU();
to open a file with the same name every time, you do

ifstream fin;
fin.open("filename.txt",ios::in);

for opening a non-constant file, such as something you got from the user:

cout >> "Enter the filename";
cin.getline(filename,80);

fin.open(filename,ios::in);

is this what you needed?

-arsenius
''after three days without programming, life becomes meaningless'' -The Tao of Programming
Advertisement
I would rather use pointers here

public: someclass(char *filename);
private : char *filename

Rest as CHollMan82 has said. You could put the opening of file into a function rather than a constructor though.
Hello from my world
Actually I'd rather use a const std::string&

  class someclass{    public:        someclass(const std::string& filename);    protected:        void open(const std::string& filename);  // should throw an exception if fails        void read();    private:        std::string   m_filename;        std::fstream  m_filestream;};someclass::someclass() : m_filename(filename){    open();}void someclass::open(){    // test to see if file is already open etc.    // open the file    m_filestream.open(m_filename, std::ios::in);    // test if the file opened successfully etc.    // throw and exception if it didn't blah blah blah }void someclass::read(){    // read the file into memory    // if the file is newline delimited you can get each line    // like this:    using std::getline;    using std::string;    string line;        while(getline(m_filestream, line))    {        // do something with the line etc    }} 


Obviously you need a destructor and the implementation could be better but you get the idea

Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on March 27, 2001 9:48:56 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
char filename[32];

ifstream file

strcpy(filename,"file.name");

file.open(filename...

You don''t need a class or fancy code. You''re also defining your char array AFTER using it.

class someclass
{
public:
someclass(char _filename[]){strcopy(filename,_filename);}
char filename[20];
};

should be (I think)

class someclass
{
public:
char filename[20];
someclass(char _filename[]){strcopy(filename,_filename);}
};

Ben
http://therabbithole.redback.inficad.com


Um, the above code isn''t called fancy, it''s called clean. const std::string accepts const char* and strings which makes it very effective.

Maybe it''s just taste or preference,

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement