Results 1 to 7 of 7

Thread: access files in the directory

  1. #1
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default access files in the directory

    i want to access and read the data of some files i.e 2 or three files from the directory
    now i am accessing all the files from the directory
    the file is text file
    plzzz help

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: access files in the directory

    Change your code so you read only the files you are interested in, instead of all of them.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: access files in the directory

    QString path=QFileDialog::getExistingDirectory();
    QDir dir(path);
    foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )
    {
    QString filename = fileinfo.filePath();
    QFile readFile(filename);
    if(!readFile.open(QFile::ReadWrite | QFile::Text ) )
    {
    qDebug("Failed to read file.....");
    //return ;
    }


    i am using foreach for access all the file of the directory
    what else i shold use to access the particular 2 or three files

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: access files in the directory

    i am using foreach for access all the file of the directory
    Well, what did you think that would do besides read every file in the directory?

    Qt Code:
    1. QString path=QFileDialog::getExistingDirectory();
    2. QDir dir(path);
    3. foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )
    4. {
    5. QString filename = fileinfo.filePath();
    6. if ( amIInterestedInThisFile( filename ) == true )
    7. {
    8. QFile readFile(filename);
    9. if( !readFile.open(QFile::ReadWrite | QFile::Text ) )
    10. {
    11. qDebug("Failed to read file.....");
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    You're going to have to write the method "bool amIInterestedInThisFile( const QString & fileName )" because only you know which two or three files you want to read.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: access files in the directory

    IT IS reading all the files one by one i want to read the files with its name i.e when i give the name of files it should read the text of that files only
    for e.g if file has starting name same then it should read all the files of the same name files after executing

    Quote Originally Posted by d_stranz View Post
    Well, what did you think that would do besides read every file in the directory?

    Qt Code:
    1. QString path=QFileDialog::getExistingDirectory();
    2. QDir dir(path);
    3. foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )
    4. {
    5. QString filename = fileinfo.filePath();
    6. if ( amIInterestedInThisFile( filename ) == true )
    7. {
    8. QFile readFile(filename);
    9. if( !readFile.open(QFile::ReadWrite | QFile::Text ) )
    10. {
    11. qDebug("Failed to read file.....");
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    You're going to have to write the method "bool amIInterestedInThisFile( const QString & fileName )" because only you know which two or three files you want to read.

    boolamiInterestedinthisfile is not declared

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: access files in the directory

    Then read the files you want, don't list all the file in the dir
    Qt Code:
    1. // QString path = QFileDialog::getExistingDirectory();
    2. // QDir dir(path);
    3. // foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )
    4.  
    5. QList<QString> files { "File1.txt", "File2.txt", "File3.txt" };
    6.  
    7. foreach (QString filename, files)
    8. {
    9. QFile readFile(filename);
    10. if(!readFile.open(QFile::ReadWrite | QFile::Text ) )
    11. {
    12. qDebug("Failed to read file.....");
    13. //return ;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    Anupamp (28th February 2017)

  8. #7
    Join Date
    Nov 2016
    Posts
    17
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: access files in the directory

    Quote Originally Posted by Santosh Reddy View Post
    Then read the files you want, don't list all the file in the dir
    Qt Code:
    1. // QString path = QFileDialog::getExistingDirectory();
    2. // QDir dir(path);
    3. // foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )
    4.  
    5. QList<QString> files { "File1.txt", "File2.txt", "File3.txt" };
    6.  
    7. foreach (QString filename, files)
    8. {
    9. QFile readFile(filename);
    10. if(!readFile.open(QFile::ReadWrite | QFile::Text ) )
    11. {
    12. qDebug("Failed to read file.....");
    13. //return ;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    thanks a LOT

Similar Threads

  1. List all XML files in a directory
    By ouekah in forum Newbie
    Replies: 4
    Last Post: 30th August 2015, 09:47
  2. Replies: 1
    Last Post: 27th December 2012, 07:01
  3. Work with directory and files
    By amiref in forum Qt Programming
    Replies: 4
    Last Post: 24th March 2011, 10:26
  4. directory and files
    By rmagro in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2008, 14:40
  5. trace new files in a directory
    By Fastman in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 13:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.