U
U
undeadter2017-09-27 23:46:28
C++ / C#
undeadter, 2017-09-27 23:46:28

How to read a text file from a zip archive using PHYSFS?

PHYSFS_init(NULL);
    PHYSFS_addToSearchPath(filename.toLatin1(), 1);

    char* myBuf;
    PHYSFS_File* myfile;

    myfile = PHYSFS_openRead("readme.txt");
    if (myfile)
    {
        myBuf = new char[PHYSFS_fileLength(myfile)];
        int length_read = PHYSFS_read(myfile, myBuf, 1, PHYSFS_fileLength(myfile));
        PHYSFS_close(myfile);
        qInfo(myBuf);
    }

Even though the file contains a single string "sdsd", with each read I get a new result:
sdsd?hq,&?
sdsd8n???,
sdsd?iL?^?{
sdsd`ri?oE
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-09-28
@undeadter

You need to close the C-string.

int length = PHYSFS_fileLength(myfile)
myBuf = new char[length + 1];
int length_read = PHYSFS_read(myfile, myBuf, 1, length);
myBuf[length_read] = 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question