View Full Version : Qt4 Gui application cannot write to file

18th December 2009, 22:30
I have a Gui application from which i have to write a timestamp along with the image number, so i have a code like this:

MyCameraWindow::MyCameraWindow(QWidget *parent) : QWidget(parent) {

timestampFile = fopen ( "timestamps.txt", "w");


void timestamp (FILE *out, int nframe){

struct tm *tmp;
time_t t;

t = time (NULL);
tmp = localtime (&t);

fprintf (out, "%4d\t%02d.%02d.%04d %02d:%02d:%02d\n", nframe, tmp->tm_mday,
tmp->tm_mon+1, tmp->tm_year+1900, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);


void MyCameraWindow::timerEvent(QTimerEvent*) {

for (int i=0; i < cameraCount; i++) cvGrabFrame (camera[i]);

if (recordFlag == 1) timestamp(timestampFile, ++frameNo);
for (int i=0; i < cameraCount; i++){


The file is being created but nothing is written, i cannot figure why! ... can someone tell me how to write to a file in Gui application?

19th December 2009, 00:38
Did you start the timer?

19th December 2009, 02:07
yes i did. Also if i put a printf statement before fprintf i can see the output.

19th December 2009, 02:33
Maybe it's time you stopped using C and started using C++ and Qt? How about replacing fprintf() with QFile? If you want to stick with C, make sure fopen() succeeds in opening the file. But it'd really be simpler if you used what Qt offers - QFile and QDateTime in this case.

19th December 2009, 10:21
I see no fclose or fflush, so the file will be empty as the data will just be buffered until then.

But I also agree with wysota - if your using Qt, you really should be using Qt classes (or at least C++) rather than standard C.