QString path = f. fileName(); QString file = path. section("/",-1,-1); QString dir = path. section("/",0,-2);
open(QIODevice::WriteOnly) and then write data to it. Similarly, to get data out of a file, you will need to call file. open(QIODevice::ReadOnly) and then read the data. You can do whatever you want with the data after that.
Try to put it in the same directory as the executable or just put the complete path into the QFile constructor. Print out the string returned by QDir::currentPath(); I bet it's different from the path where the trace. txt file is located at.
QFile is an I/O device for reading and writing text and binary files and resources. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream. The file name is usually passed in the constructor, but it can be set at any time using setFileName().
Use a QFileInfo
to strip out the path (if any):
QFileInfo fileInfo(f.fileName());
QString filename(fileInfo.fileName());
One approach, not necessarily the best: from a QFile
, you can get the file specification with QFile::fileName()
:
QFile f("/home/umanga/Desktop/image.jpg");
QString str = f.fileName();
then you can just use the string features like QString::split
:
QStringList parts = str.split("/");
QString lastBit = parts.at(parts.size()-1);
just in addition: to seperate filename and file path having QFile f
QString path = f.fileName();
QString file = path.section("/",-1,-1);
QString dir = path.section("/",0,-2);
you don't need to create an additional fileInfo.
I use this:
bool utes::pathsplit(QString source,QString *path,QString *filename)
{
QString fn;
int index;
if (source == "") return(false);
fn = source.section("/", -1, -1);
if (fn == "") return(false);
index = source.indexOf(fn);
if (index == -1) return(false);
*path = source.mid(0,index);
*filename = fn;
return(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With