{"id":23344395,"url":"https://github.com/rec/tfile","last_synced_at":"2025-04-10T02:32:55.366Z","repository":{"id":137549519,"uuid":"46237405","full_name":"rec/tfile","owner":"rec","description":"📁 tiny C++11 file utilities 📁","archived":false,"fork":false,"pushed_at":"2019-02-02T19:36:42.000Z","size":928,"stargazers_count":22,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-24T04:04:07.185Z","etag":null,"topics":["file-handling","files","raii","tfile"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-15T21:48:19.000Z","updated_at":"2023-12-07T22:09:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"c4af5253-21d8-4430-a292-3bff5b112288","html_url":"https://github.com/rec/tfile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rec%2Ftfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rec%2Ftfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rec%2Ftfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rec%2Ftfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rec","download_url":"https://codeload.github.com/rec/tfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144299,"owners_count":21054904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["file-handling","files","raii","tfile"],"created_at":"2024-12-21T06:26:31.008Z","updated_at":"2025-04-10T02:32:55.358Z","avatar_url":"https://github.com/rec.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"tfile - tiny C++11 file utilities\n---------------------------\n\ntfile is a tiny header-only library for C++11 and beyond which offers a small\nnumber of essential features:\n\n  * functions to read a whole file at once, write a whole file at once,\n    and get the bytesize of a file;\n\n* and Openers: zero-cost abstractions that wrap C's FILE* classic file\n   handle\n\nThe functions\n=================\n\nThe functions:\n\n* Read an entire file at once\n`std::string tfile::read(char const* filename)`\n\n* Write a sequence of bytes in memory to a file\n`size_t write(char const* filename, char const* data, size_t length)`\n\n* Write a null-terminated string to a file\n`size_t write(char const* filename, char const* data)`\n\n* Write a std::string to a file\n`size_t write(char const* filename, const std::string\u0026 s)`\n\n* Write a container of strings, adding line endings\n`template \u003ctypename C\u003e size_t writeLines(const char* filename, C);`\n\n* Write a begin, end range of strings, adding line endings\n`template \u003ctypename It\u003e size_t writeLines(const char* filename, It begin, It end);`\n\n* Get the size in bytes of a file\n`size_t tfile::size()`\n\nExamples of usage:\n\n    auto data = tfile::read(\"myfile.txt\");\n    data += \"another line\\n\";\n    tfile::write(\"myfile.txt\", data);\n    std::cout \u003c\u003c \"myfile.txt: filesize=\" \u003c\u003c tfile::size(\"myfile.txt\");\n\n    // Writes a file with three lines, using the line endings of the platform\n    tfile::writeLines(\"myfile.txt\", {\"line1\", \"line2\", \"line3\"});\n\nFile Openers\n==================\n\nFile Openers are for applications which need to keep a file open for\nprocessing. File Openers are a thin wrapper over the C file handle type FILE*,\nwhich is the basis of I/O in C and C++.\n\nA File Opener offers these advantages over a raw FILE*:\n\n  * Automatically closes the FILE* in its destructor\n  * Prevents impossible reads or writes at compile time\n  * Throws an exception if the file won't open and exceptions are enabled\n  * Can iterate one line at a time\n\n\"Impossible reads or writes\" means that there is nothing in C or C++ to\nprevent writing code to, say, read from a write-only file handle - it will\nsimply return an error at runtime - but a File Opener provides read or\nwrite methods only if they actually work, so these errors can be caught at\ncompile-time.\n\nThere are six File Openers, corresponding to the six modes in which files\ncan be opened:\n\n  * `tfile::Reader`: read-only, position at start of file - mode `\"r\"`\n  * `tfile::ReaderWriter`: read-write, position at start of file - mode `\"r+\"`\n  * `tfile::Writer`: write-only, truncate file to empty - mode `\"w\"`\n  * `tfile::TruncateReaderWriter`: read-write, truncate to empty - mode `\"w+\"`\n  * `tfile::Appender`: write-only, position at end of file - mode `\"a\"`\n  * `tfile::ReaderAppender`: read-write, position at end of file - mode `\"a+\"`\n\nFor the exact signatures of methods, see the file\n[tfile.h](https://github.com/rec/tfile/blob/master/include/tfile/tfile.h).\n\nFor more information on file opening modes, see\nhttp://man7.org/linux/man-pages/man3/fopen.3.html\n\nExamples of usage:\n\n    tfile::Reader reader(\"myfile2.txt\");\n    std::string s(10);\n    auto bytes_read = reader.read(s);   // at most 10\n\n    // You can't write a reader, so this won't compile:\n    // reader.write(\"hello\");\n\n    // Open a file, appear a line, close it.\n    tfile::Appender(\"myfile2.txt\").writeLine(\"a new line\");\n\n    // You can't read an Appender, so this won't compile:\n    // tfile::Appender(\"myfile2.txt\").read();\n\n    // Iterate through lines.\n\n    std::string line;\n    while (reader.readLine(line)) {\n       // Do things to `line` here\n    }\n\n    // Another way to do that:\n\n    reader.forEachLine([] (const std::string\u0026 line) {\n        // Do things to `line` here\n    });\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frec%2Ftfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frec%2Ftfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frec%2Ftfile/lists"}