PDA

View Full Version : Working with files - use in different functions?



Splatify
13th February 2011, 12:01
Hi all I'm not too good at working with files, they confuse the hell out of me. However i have this class, let call it test.cpp




#include <iostream>
#include <string>
#include <fstream>
#include <QtGui>

using namespace std;


class test {

private:


public:
ifstream myfile;
bool open_file();
void close_file();
void next_question();
};

bool open_file(){

myfile.open("C:\\test.txt");
if(myfile.is_open()){
qDebug() << "Open";
return true;
}
else
{
qDebug() << "Error - file not opened";
return false;
}
}

void close_file(){

myfile.close;

}


void next_line(){

if(open_file()){

// Code here to do stuff with myfile

}
else
{
qDebug() << "File not opened";
}


}




The error i keep getting is:

" 'myfile' was not declared in this scope. "

How do i work with myfile across all of my functions within a class?


Thanks for your time and trouble

Lykurg
13th February 2011, 12:08
You might want to read about basic C++ stuff such as scopes. It has to be something like
bool test::open_file(){

Zlatomir
13th February 2011, 12:11
And since you are using Qt you can use QFile (http://doc.qt.nokia.com/4.7/qfile.html)

Splatify
13th February 2011, 17:53
Thanks for all those who replied. I have got it working now and feel like I have a much better understanding now :)

Thanks