Search code examples
clinuxctest

Where to create a temporary file from within a test


I have a test, which is run during the standard cmake/ctest process.

The problem is that my test needs to create a temporary file (no need to preserve it across different tests), and it fails with the EACCES error code.

The following (presumably) fails:

m_hFile = open("/tmp/mytest.bin", O_RDWR | O_CREAT);

Do I have to tweak something related to the permissions, or perhaps write to another location?


Solution

  • Thanks everybody, I found the problem.

    Turns-out that in case the file doesn't exist and should be created - it seems that I must specify the mode, and use the 3-parameter version of open.

    m_hFile = open("/tmp/mytest.bin", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);