×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    QFile can not open file ,WHY???

    I want to save something in local directory at MyDocs/

    This is my code,the first code below is correct and add and create txt and content to it.
    However the second code went wrong.
    Code:
    QDir::setCurrent("/home/user/MyDocs/Documents/");   //Go to this directory
    QFile file("lovenote_exp.txt");          //open file,all done correctly
        if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
            qDebug()<< "Cannot open file for writing: " ;
            return;
        }
    
        QTextStream out(&file);
        out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
       file.close();
    The below is the second code, it went wrong.
    Code:
    QDir::setCurrent("/home/user/MyDocs/Documents/");   //Go to this directory
    QString myfilename="myfile.txt";      // create a QString variant with this value.
    QFile file(myfilename);          // this is the difference,it went wrong there
        if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
            qDebug()<< "Cannot open file for writing: " ;
            return;
        }
    
        QTextStream out(&file);
        out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
       file.close();
    To be truth,i really dont know what went wrong ....But the second codes still didnt work .Anyone could help me???
    It makes me confused.
    Help!!!

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    105

    Re: QFile can not open file ,WHY???

    Does the file exist in /home/user/MyDocs/Documents/?
    It clearly says you open for writeonly + append; but you do not give details on how to behave if the file does *not* exist...

    Quote Originally Posted by rhythmkay View Post
    I want to save something in local directory at MyDocs/


    This is my code,the first code below is correct and add and create txt and content to it.
    However the second code went wrong.
    Code:
    QDir::setCurrent("/home/user/MyDocs/Documents/");   //Go to this directory
    QFile file("lovenote_exp.txt");          //open file,all done correctly
        if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
            qDebug()<< "Cannot open file for writing: " ;
            return;
        }
    
        QTextStream out(&file);
        out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
       file.close();
    The below is the second code, it went wrong.
    Code:
    QDir::setCurrent("/home/user/MyDocs/Documents/");   //Go to this directory
    QString myfilename="myfile.txt";      // create a QString variant with this value.
    QFile file(myfilename);          // this is the difference,it went wrong there
        if (!file.open(QIODevice::WriteOnly|QIODevice::Append)) {
            qDebug()<< "Cannot open file for writing: " ;
            return;
        }
    
        QTextStream out(&file);
        out<<"\t"<<date<<"\t"<<title<<endl<<"\t"<<text<<endl;
       file.close();
    To be truth,i really dont know what went wrong ....But the second codes still didnt work .Anyone could help me???
    It makes me confused.
    Help!!!

Similar Threads

  1. QFile problem
    By BalajiR in forum Qt
    Replies: 9
    Last Post: 2010-03-04, 05:49
  2. QFile::seek: IODevice is not open
    By rongyihit in forum [Archived] Other Programming Discussion 关于其他编程技术的讨论
    Replies: 0
    Last Post: 2009-08-18, 09:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •