PDA

View Full Version : access files in the directory



Anupamp
27th February 2017, 16:06
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

d_stranz
27th February 2017, 16:44
Change your code so you read only the files you are interested in, instead of all of them.

Anupamp
27th February 2017, 17:08
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

d_stranz
27th February 2017, 20:31
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?



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


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.

Anupamp
28th February 2017, 05:54
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


Well, what did you think that would do besides read every file in the directory?



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


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

Santosh Reddy
28th February 2017, 06:08
Then read the files you want, don't list all the file in the dir


// QString path = QFileDialog::getExistingDirectory();
// QDir dir(path);
// foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )

QList<QString> files { "File1.txt", "File2.txt", "File3.txt" };

foreach (QString filename, files)
{
QFile readFile(filename);
if(!readFile.open(QFile::ReadWrite | QFile::Text ) )
{
qDebug("Failed to read file.....");
//return ;
}
}

Anupamp
28th February 2017, 06:47
Then read the files you want, don't list all the file in the dir


// QString path = QFileDialog::getExistingDirectory();
// QDir dir(path);
// foreach (QFileInfo fileinfo, dir.entryInfoList(QDir::Files) )

QList<QString> files { "File1.txt", "File2.txt", "File3.txt" };

foreach (QString filename, files)
{
QFile readFile(filename);
if(!readFile.open(QFile::ReadWrite | QFile::Text ) )
{
qDebug("Failed to read file.....");
//return ;
}
}

thanks a LOT