added writing capacilities to files and made the appropriate changes to the filedemo.c file

This commit is contained in:
Odweta
2023-12-23 18:28:58 +01:00
parent b9c93116bc
commit 0c68944a1d
9 changed files with 172 additions and 96 deletions
+15 -12
View File
@@ -1,21 +1,24 @@
#include <odweta/vector.h>
#include <odweta/file.h>
#include <stdio.h>
int main() {
printf("creating a vector using 'vectorInit(<type>)' (returns a vector_t*)\npossible type are: INT, FLOAT, CHAR, VECTOR (LIST in the future)\n");
vector_t* v = vectorInit(INT);
printf("creating a file using 'fileInit(<filename>)' (returns a file_t*)\n");
file_t* f = fileInit("test.txt");
printf("pushing an integer value into the vector\nnote the kind of unusual way of passing the number argument: if you want to pass an expression (e.g. the value '1' or '4*4'), something not stored in a variable, you follow this syntax - &(type){value} (e.g. &(int){1 + 1})\n->'vectorPush(vector_t*, &(int){123})'\n");
vectorPush(v, &(int){123});
vectorPrint(v);
printf("printing the file contents\n\n");
filePrint(f);
printf("popping the last value of the vector; in this case, it is an integer, so you type: 'int poppedValue = *((int *)vectorPop(vector_t*))'\n");
int poppedValue = *((int *)vectorPop(v));
printf("popped: %d\n", poppedValue);
vectorPrint(v);
printf("\nconvering the file contents into a string (char*)\n");
char* string = fileToString(f);
printf("\"%s\"\n", string);
printf("if you are finished working with the vector BEFORE the immediate end of the program, run 'vectorCleanup(vector_t*)', otherwise it is not strictly necessary\n");
vectorCleanup(v);
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);
return 0;
}