diff --git a/.gitignore b/.gitignore index d6d4cf1..958b0ca 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ #vscode .vscode + +writeTest \ No newline at end of file diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 5c7863a..301cf17 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -26,7 +26,11 @@ File::File(std::string path, char mode, uint64_t cursorPosition): mode(mode), path(path), cursorPosition(cursorPosition){ std::filesystem::path filePath = path; - this->size = ErrorOr(std::filesystem::file_size(filePath)); + if(this->mode == 'w' || this->mode == 'a'){ + this->size = ErrorOr(0); + }else{ + this->size = ErrorOr(std::filesystem::file_size(filePath)); + } this->open(); } @@ -166,7 +170,7 @@ ErrorOrVoid File::writeString(tiny_utf8::string data){ } ErrorOr File::open(std::string path, char mode, uint64_t startPosition){ - if (!std::filesystem::exists(path)) { + if (!std::filesystem::exists(path) && (mode == 'r' || mode == 'm')) { return ErrorOr(true, ErrorCodes::FILE_NOT_FOUND, nullptr); } diff --git a/src/test/file.cpp b/src/test/file.cpp index ea49669..c3e26ab 100644 --- a/src/test/file.cpp +++ b/src/test/file.cpp @@ -48,11 +48,11 @@ int main(){ File *readFile; //writeByte test - writeFile = File::open("resources/unicode_data/writeTest", 'w').value; + writeFile = File::open("resources/writeTest", 'w').value; writeFile->writeByte('a'); writeFile->close(); - readFile = File::open("resources/unicode_data/writeTest", 'r').value; + readFile = File::open("resources/writeTest", 'r').value; uint8_t testByte = readFile->readByte().value; ASSERT(testByte == 'a'); std::cout << "Passed write byte test." << std::endl;