made work: count the number of words in a file_t*

This commit is contained in:
Odweta
2023-12-26 09:50:18 +01:00
parent cb04f9a87c
commit f1444de526
5 changed files with 110 additions and 17 deletions
+76 -16
View File
@@ -1,30 +1,90 @@
#include <odweta/list.h> // #include <stdio.h>
// #include <odweta/util.h>
//
// int main() {
// char* string = intToString(1729843);
//
// printf("final string: %s\n", string);
//
// char* sstring = intToString(123);
//
// printf("final string: %s\n", sstring);
//
// return 0;
// }
#include <odweta/file.h>
#include <stdio.h> #include <stdio.h>
int main() { int main() {
list_t* l = listInit(); printf("creating a file using 'fileInit(<filename>)' (returns a file_t*)\n");
file_t* f = fileInit("test.txt");
listPush(l, INT, &(int){5}); printf("printing the file contents\n\n");
listPush(l, INT, &(int){2}); filePrint(f);
listPush(l, INT, &(int){1});
listSetAt(l, 1, FLOAT, &(float){3.14}); printf("\nconvering the file contents into a string (char*)\n");
char* string = fileToString(f);
printf("\"\"\"\n%s\"\"\"\n", string);
printf("value at index 2 in list l:\n"); 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");
int val = *((int*)listGetAt(l, 2)); fileWrite(f, "'this will not end with a \"\\n\"' ");
printf("%d\n", val); fileWriteln(f, "this line was appended");
filePrint(f);
list_t* m = listInit(); 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);
listPush(m, CHAR, &(char){'a'}); 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");
listPush(m, CHAR, &(char){'b'});
listPush(m, CHAR, &(char){'c'});
listSetAt(l, 2, LIST, m); file_t* src = fileInit("test.txt");
listPrint(l); fileCopy(src, "copy_of_test.txt");
listCleanup(l); printf("src:\n");
filePrint(src);
printf("\ndst:\n");
file_t* dst = fileInit("copy_of_test.txt");
filePrint(dst);
fileMove(dst, "moved_copy_of_test.txt");
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);
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");
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 with prepended numbers:\n");
filePrint(g_copy);
fileCleanup(g);
//fileCleanup(g_copy);
printf("counting words in a file\n");
file_t* words = fileInit("test.txt");
int word_count = fileCountWords(words);
printf("word count of file %s is %d\n", words->path, word_count);
fileCleanup(words);
return 0; return 0;
} }
+9
View File
@@ -77,5 +77,14 @@ int main() {
fileCleanup(g); fileCleanup(g);
//fileCleanup(g_copy); //fileCleanup(g_copy);
printf("counting words in a file\n");
file_t* words = fileInit("test.txt");
int word_count = fileCountWords(words);
printf("word count of file %s is %d\n", words->path, word_count);
fileCleanup(words);
return 0; return 0;
} }
+2
View File
@@ -34,4 +34,6 @@ void fileCreateEmpty(char* path); // create an empty file with the given name
void fileNumberLines(file_t* f); // prepend line numbers in front of each line in a file void fileNumberLines(file_t* f); // prepend line numbers in front of each line in a file
int fileCountWords(file_t* f); // counts the number of words in a file
#endif #endif
+21
View File
@@ -256,3 +256,24 @@ fileNumberLines(file_t* f) {
fclose(fd); fclose(fd);
} }
int
fileCountWords(file_t* f) {
int num_words = 0;
char prev;
for (int i = 0; i < f->length; i++) {
for (int j = 0; j < ((vector_t*)vectorGetAt(f->lines, i))->size; j++) {
char ch = *((char*)vectorGetAt((vector_t*)vectorGetAt(f->lines, i), j));
if (
(ch == ' ' && prev != ' ') ||
(i == f->length-1 && j == ((vector_t*)vectorGetAt(f->lines, i))->size-1) ||
(j == ((vector_t*)vectorGetAt(f->lines, i))->size-1 && j != 0)
) {
num_words++;
}
prev = ch;
}
}
return num_words;
}
+2 -1
View File
@@ -4,4 +4,5 @@ and
this was an empty line this was an empty line
!#%!@# <- symbols! !#%!@# <- symbols!
about 1100 lines of implementation code, not including .h files and demos nad Makefiles and stuff like that 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