PDA

View Full Version : How to access the same file in different scopes within a function



rookee
10th December 2015, 20:13
Hello All,

Please take a look at the below code. I posted a question earlier but had some changes after that.


FILE *filename1;


void MainWindow::on_checkBox_clicked()
{
QFile file(QString fname);//LINE1

if(ui->checkBox->isChecked())
{

filename = QFileDialog::getSaveFileName(...);//LINE2
if(!filename.isEmpty())
{
if(QFile::exists(filename)){

//Append 2 empty lines and append data(some data)
}else { // I want to access the filename from LINE2 create a file with name entered by user and append some data
file(filename);
file.open(QIODevice::Append);
//
// Data to be appended
//
}
}
}
}

Can someone please help me understand how to access filename from LINE2 and do some stuff on that file in a different location within the function on_checkBox_clicked(). I'm trying to declare a file in LINE1 and use it to perform different checks, this is throwing some errors. Please explain the syntax for using the filename in a different scope/sub-scope(or what ever they call it) within the same function with above example. And if I want to access the same file in different function of my project, how can I access it. Thanks in advance.

ChrisW67
10th December 2015, 20:33
Have you read the errors? You will likely find that they tell you why line 6 is not what you expected. It reads like a declaration of a function called file() that takes a QString argument and returns a QFile, and not a local variable of type QFile. Remove the stuff from ( to ) and see how much further you get. You then need to focus on lne 18.