void create_if_not_exists(const NString &name, const NString &license, bool force) { NFile outf; NDataStream content; if (NFile::exists(name) && !force) { NMessage::print() << "File " << name << " already exists."; return; } outf.setFileName(name); outf.open(NIODevice::Truncate); if (license != "") { content = content + license + "\n"; } content = content + "// Source code file created by nproj " + N_TOOLS_VERSION + " with NUS version " + N_VERSION + "\n"; outf.write(content); outf.closeDevice(); }
int main(void) { NFile tmp; tmp.setFileName("testfile.tmp"); if (tmp.exists()) { NMessage::print() << "File " << tmp.getFileName() << " does not exist"; } try { // Opening a file may throw a NIOException exception // Put file in read-only mode to see it happening tmp.open(NIODevice::ReadWrite); // If the file was openned, write something to it tmp.write("test"); } catch (NIOException &e) { NMessage::print() << "Error: " << e.getDescription(); if (e.getResource() == NIOException::FILE) { NMessage::print() << "On file: " << e.getResourceName(); } if (e.getFlag() == NIOException::IO_ACCESS_DENIED) { NWarning::print() << "Access Denied"; } exit(1); } return 0; }