Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a temporary text file in QT and then delete it at the end of the program. I have two threads(T1, T2). T1 need to write the text in TEMP file and and T2 will read it continuously. I need to keep tracking the file pointer as well. I haven't had much luck with Google.

Could you tell me how to go for it?

*NOTE, i'm not a professional, so please bear the pain of making it as simple as possible

What I have tried:

I had done this all with writing the text file and than reading it later. But this is creating problem now solution for the problem is generate the Temporary file. But dont know how to write text into TEMP file and how to fetch text.
Posted
Updated 2-Mar-16 23:55pm
v2

1 solution

When multiple threads should access the same object you must use locking. For your case of a temporary file I would create a class that encapsulates all the necessary operations (including deleting the file in the destructor).

Example (using C library FILE object and QMutex Class | Qt Core 5.5[^] lock object):
C++
// TempFile.h
#include <QMutex>
#include <stdio.h>

class TempFile
{
public:
    TempFile();
    ~TempFile();
    bool Open(const QString& fileName);
    void Close();
    int Read(char * data, int size);
    int Write(const char * data, int size);
protected:
    QString name;
    FILE *file;
    QMutex mutex;
    long pos;
};


C++
// TempFile.cpp
#include "TempFile.h"
TempFile::TempFile()
{
    file = NULL;
    pos = 0;
}

TempFile::~TempFile()
{
    bool isOpen = file != NULL;
    Close();
    if (isOpen)
        unlink(QString::toLocal8Bit(name).data());
}

bool TempFile::Open(const QString& fileName)
{
    file = fopen(QString::toLocal8Bit(fileName).data(), "w+");
    name = fileName;
    return file != NULL;
}

void TempFile::Close()
{
    if (file)
        fclose(file);
    file = NULL;
    pos = 0;
}

int TempFile::Read(char * data, int size)
{
    mutex.lock();
    // Seek to read position
    fseek(file, pos, SEEK_SET);
    int read = fread(data, 1, size, file);
    if (read > 0)
        pos += read;
    mutex.unlock();
    return read;
}

int TempFile::Write(const char * data, int size)
{
    mutex.lock();
    // Seek to write position (end)
    fseek(file, 0, SEEK_END);
    int written = fwrite(data, 1, size, file);
    mutex.unlock();
    return written;
}
 
Share this answer
 
v4
Comments
XamBEE 3-Mar-16 6:09am    
Thanks for your help. I have one question that what about QTemporaryFile class?? Do you think it would difficult with QTemporaryFile class?
Jochen Arndt 3-Mar-16 6:11am    
If it fits your needs you may use it but should derive your own class which implements the read and write functions with locking as shown in my solution.
XamBEE 3-Mar-16 7:59am    
would be nice if you can guide that how to read data from Temporary file using QTemporaryFile. How one can get the address/access of the Temporary file??
If data is e.g
aa, bbb, 123, d,
bb, ddd, 456, e,
i want to read data Qoma(,) separately
Jochen Arndt 3-Mar-16 8:18am    
QTemporaryFile inherits QFile which inherits QIODevice.
So you can use the corresponding QFile and/or QIODevice functions:
fopen -> open
fclose -> close
fread -> readData
fwrite -> writeData
fseek -> seek

But there is no seek() option to set the position to the end of file. So you must track the write position yourself similar to the read position (initialise with zero upon opening and increment by the number of bytes written with each writing).
XamBEE 8-Mar-16 3:56am    
is this the correct way to write into the tamp file??
//from Thread-1 class i want to write into the Temp file.
const char* Cardata[] = {"Blue car", "brown", "1996"};
bool fileopned= Tempfile::open(file);
if(fileopned==true){
Tempfile::write(Cardata,size);
}
Tempfile::close();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900