From 5374cf71914089fb934bd8100cc4f09d9c6d1458 Mon Sep 17 00:00:00 2001 From: Odweta Date: Fri, 29 Dec 2023 17:20:53 +0100 Subject: [PATCH] made work: count the number of words in a file --- README.md | 2 +- demo.c | 110 +++++++++++++++++++++++++++++++----------------------- test.txt | 1 + 3 files changed, 65 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 5dd4c92..c4653af 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,10 @@ To create a project that uses this library easier, use the `vendetta` utility (` 6. Deleting a file 7. Checking if a file exists 8. Add line numbers at the beggining of each line in a file (with correct indentation) +9. Number of words in a file #### TODO: -1. Number of words in a file + You tell me diff --git a/demo.c b/demo.c index 58ada31..b3ca46e 100644 --- a/demo.c +++ b/demo.c @@ -17,74 +17,90 @@ #include int main() { - printf("creating a file using 'fileInit()' (returns a file_t*)\n"); - file_t* f = fileInit("test.txt"); + printf("creating a file using 'fileInit()' (returns a file_t*)\n"); + file_t *f = fileInit("test.txt"); - printf("printing the file contents\n\n"); - filePrint(f); + printf("printing the file contents\n\n"); + filePrint(f); - printf("\nconvering the file contents into a string (char*)\n"); - char* string = fileToString(f); - printf("\"\"\"\n%s\"\"\"\n", string); + printf("\nconvering the file contents into a string (char*)\n"); + char *string = fileToString(f); + printf("\"\"\"\n%s\"\"\"\n", string); - printf("writing is quite simple, just call the 'fileWrite(file_t*, char*)' or 'fileWriteln(file_t*, char*)' function, where the second argument is the string you want to write into a file\nthe 'fileWriteln()' function writes the specified string AND a '\\n' after it, whereas the 'fileWrite()' function does not append the '\\n'\n"); - fileWrite(f, "'this will not end with a \"\\n\"' "); - fileWriteln(f, "this line was appended"); - filePrint(f); + printf("writing is quite simple, just call the 'fileWrite(file_t*, char*)' " + "or 'fileWriteln(file_t*, char*)' function, where the second argument " + "is the string you want to write into a file\nthe 'fileWriteln()' " + "function writes the specified string AND a '\\n' after it, whereas " + "the 'fileWrite()' function does not append the '\\n'\n"); + fileWrite(f, "'this will not end with a \"\\n\"' "); + fileWriteln(f, "this line was appended"); + filePrint(f); - printf("if you are finished working with the file BEFORE the immediate end of the program, run 'fileCleanup(file_t*)', otherwise it is not strictly necessary\nit will, though, append a '\\n' at the end of the file if you have not done so, making writing into files more consistent, so for this reason, I recommend you use it with every file_t*\n"); - fileCleanup(f); + printf( + "if you are finished working with the file BEFORE the immediate end of " + "the program, run 'fileCleanup(file_t*)', otherwise it is not strictly " + "necessary\nit will, though, append a '\\n' at the end of the file if " + "you have not done so, making writing into files more consistent, so for " + "this reason, I recommend you use it with every file_t*\n"); + fileCleanup(f); - printf("copying and moving\nafter this block of code, the contents of test.txt will have been copied into copy_of_test.txt and moved to moved_copy_of_test.txt, which means that copy_of_test.txt is deleted and the contents of test.txt are only in test.txt and moved_copy_of_test.txt\n"); + printf("copying and moving\nafter this block of code, the contents of " + "test.txt will have been copied into copy_of_test.txt and moved to " + "moved_copy_of_test.txt, which means that copy_of_test.txt is deleted " + "and the contents of test.txt are only in test.txt and " + "moved_copy_of_test.txt\n"); - file_t* src = fileInit("test.txt"); + file_t *src = fileInit("test.txt"); - fileCopy(src, "copy_of_test.txt"); + fileCopy(src, "copy_of_test.txt"); - printf("src:\n"); - filePrint(src); + printf("src:\n"); + filePrint(src); - printf("\ndst:\n"); - file_t* dst = fileInit("copy_of_test.txt"); - filePrint(dst); + printf("\ndst:\n"); + file_t *dst = fileInit("copy_of_test.txt"); + filePrint(dst); - fileMove(dst, "moved_copy_of_test.txt"); + fileMove(dst, "moved_copy_of_test.txt"); - printf("moved file:\n"); - file_t* move_dst = fileInit("moved_copy_of_test.txt"); - filePrint(move_dst); + printf("moved file:\n"); + file_t *move_dst = fileInit("moved_copy_of_test.txt"); + filePrint(move_dst); - fileCleanup(src); - printf("DO NOT cleanup a deleted file! (dst, in this case) it is already done for you by the 'fileMove(file_t*, char*)' function\n"); - // fileCleanup(dst); - fileCleanup(move_dst); + fileCleanup(src); + printf("DO NOT cleanup a deleted file! (dst, in this case) it is already " + "done for you by the 'fileMove(file_t*, char*)' function\n"); + // fileCleanup(dst); + fileCleanup(move_dst); - printf("when copying and moving files, the first argument is a file_t* (source file) and the second argument is a char* (the path of the destination file), not a file_t*!\n"); + printf("when copying and moving files, the first argument is a file_t* " + "(source file) and the second argument is a char* (the path of the " + "destination file), not a file_t*!\n"); - printf("prepending line numbers at the beggining of each line in a file\n"); + printf("prepending line numbers at the beggining of each line in a file\n"); - file_t* g = fileInit("test.txt"); - fileCopy(g, "copy_of_test.txt"); - file_t* g_copy = fileInit("copy_of_test.txt"); - fileNumberLines(g_copy); + file_t *g = fileInit("test.txt"); + fileCopy(g, "copy_of_test.txt"); + file_t *g_copy = fileInit("copy_of_test.txt"); + fileNumberLines(g_copy); - printf("file withOUT prepended numbers:\n"); - filePrint(g); + printf("file withOUT prepended numbers:\n"); + filePrint(g); - printf("file with prepended numbers:\n"); - filePrint(g_copy); + printf("file with prepended numbers:\n"); + filePrint(g_copy); - fileCleanup(g); - //fileCleanup(g_copy); + fileCleanup(g); + // fileCleanup(g_copy); - printf("counting words in a file\n"); + printf("counting words in a file\n"); - file_t* words = fileInit("test.txt"); + file_t *words = fileInit("test.txt"); - int word_count = fileCountWords(words); - printf("word count of file %s is %d\n", words->path, word_count); + int word_count = fileCountWords(words); + printf("word count of file %s is %d\n", words->path, word_count); - fileCleanup(words); + fileCleanup(words); - return 0; + return 0; } diff --git a/test.txt b/test.txt index 57a33df..bd93a05 100644 --- a/test.txt +++ b/test.txt @@ -6,3 +6,4 @@ this was an empty line !#%!@# <- symbols! about 1100 lines of implementation code, not including .h files and demos and Makefiles and stuff like that 'this will not end with a "\n"' this line was appended +'this will not end with a "\n"' this line was appended