This commit is contained in:
2025-10-08 16:11:15 +02:00
parent 6ea2416f9c
commit 728b639857
8 changed files with 373 additions and 0 deletions

44
src/getfuncs.c Normal file
View File

@@ -0,0 +1,44 @@
#include "getfuncs.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int getFileLength(const char *filename) {
FILE* stream = fopen(filename, "r");
if (!stream) return -1;
int file_length = 0;
char buffer[128];
while (fgets(buffer, sizeof(buffer), stream)) {
file_length++;
}
fclose(stream);
return file_length;
}
int fetchWord(const char *filename, char *fullWord, int file_length) {
if (file_length <= 0) return -1;
srand(time(NULL));
int random_line = rand() % file_length;
FILE* stream = fopen(filename, "r");
if (!stream) return -1;
int current = 0;
while (fgets(fullWord, 128, stream)) {
if (current == random_line) {
// Retirer le '\n' si présent
char *newline = strchr(fullWord, '\n');
if (newline) *newline = '\0';
fclose(stream);
return 0;
}
current++;
}
fclose(stream);
return -1;
}