forked from menault/TD3_DEV51_Qualite_Algo
add src
This commit is contained in:
44
src/getfuncs.c
Normal file
44
src/getfuncs.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user