Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 LE
My Carrier: Three

QFile::size() always returning 0 bytes

Hi,

I'm having a bit of an unusual problem at the moment in a background app where I can't obtain the size of a generated file written to the device's /data directory. The file is successfully written and shows up in the directory explorer view, with a filesize of ~100kb. Getting the file info using QFileInfo returns correct information about its path, extension, last modified time etc. However, calling the size() function always returns 0.

 

I tried to get around this by using the old-fashioned fopen and ftell functions, but that also returns 0 bytes. This is very unusual as if ftell fails it returns -1, not 0.

 

I have no idea where this is going wrong, and am wondering if this is an OS bug. I've not found any solutions on this forum or by searching Google. Here's a redacted version of the code I'm using:

			
QFileInfo file(aFilePath); if(file.exists() && file.isFile()) {

// Have to do it the old-fashioned way as QFile::size() is broken FILE* cFile = fopen(file.absoluteFilePath().toAscii(), "r"); long size = ftell(cFile); fclose(cFile); qDebug() << "File size is "<< size;

// Getting file info via QFileInfo() file.refresh(); qDebug() << "Successfully obtained info for file " << file.filePath(); qDebug() << "Absolute file path is " << file.absoluteFilePath(); qDebug() << "File suffix is " << file.suffix(); qDebug() << "Last modified: " << file.lastModified().toString("yyyy-MM-dd @ hh:mm:ss"); qDebug() << "Direct size is "<< QFile(file.absoluteFilePath()).size(); qDebug() << "File size is "<< dataSize;
// For comparison, check the size of a directory QFileInfo dataDir(QDir::homePath()); qDebug() << "Data directory size is "<< dataDir.size();

 

Ths produces the following logging output:

 

File size is  0 
Successfully obtained info for file  "data/150414_162144.txt" 
Absolute file path is  "/accounts/1000/appdata/myRedactedAppDir/data/150414_162144.txt" 
File suffix is  "txt" 
Last modified:  "2014-04-15 @ 16:21:44" 
Direct size is  0 
File size is  0 
Data directory size is  4096

 

I'm at a loss as to why this is happening, any suggestions?



Did this post help you? If so, please make sure to "like" it!
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: QFile::size() always returning 0 bytes

[ Edited ]

Hi!

 

Fseek call is missing. Also, it's better to use binary file mode ("rb").

 

FILE *f = fopen("filename", "rb");
if (!f) {
  // Handle error
}

if (fseek(f, 0L, SEEK_END) != 0) {
  // Handle error
}
 
long fileSize = ftell(f);
if (fileSize == -1) {
  // Handle error
}

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 LE
My Carrier: Three

Re: QFile::size() always returning 0 bytes

Nope, still returning 0 bytes with that method. Also zero when I use QFile::size().


Did this post help you? If so, please make sure to "like" it!