Files
TD3_DEV51_Qualite_Algo/getfuncs.c

80 lines
1.2 KiB
C
Raw Normal View History

2025-10-08 11:56:36 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
*/
int getFileLength(){
/*variables*/
FILE* stream;
int length = 0;
char* line = NULL;
size_t len = 0;
ssize_t read;
/*prog*/
stream = fopen("wordbank.txt", "r");
if (stream == NULL){
return EXIT_FAILURE;
}
while((read = getline(&line, &len, stream)) != -1){
length++;
}
fclose(stream);
return length-1;
}
/*
*/
int fetchWord(char* fullword, int file_length){
/*variables*/
FILE* stream;
int random = (rand() * time(NULL)) % (file_length);
2025-10-08 12:35:01 +02:00
char read[8];
2025-10-08 11:56:36 +02:00
unsigned int counter = 0;
int char_size = (int) sizeof(char);
2025-10-08 12:35:01 +02:00
int lign=0;
int offset=0;
2025-10-08 11:56:36 +02:00
/*prog*/
printf("%d\n", random);
stream = fopen("wordbank.txt", "r");
if (stream == NULL){
return -1;
}
2025-10-08 12:35:01 +02:00
for(lign=0; lign<random; lign++){
while(*read!='\n'){
fread(read, sizeof(char), 1, stream);
offset++;
}
*read=' ';
}
2025-10-08 11:56:36 +02:00
while(*read!='\n'){
2025-10-08 12:35:01 +02:00
fread(read, sizeof(char), 1, stream);
2025-10-08 11:56:36 +02:00
fullword[counter] = *read;
counter++;
}
fclose(stream);
return 0;
}
int main(void){
/*variables*/
int flength;
char fullword[128];
/*prog*/
flength = getFileLength();
if(fetchWord(fullword, flength)!=-1){
2025-10-08 12:35:01 +02:00
printf("%s", fullword);
2025-10-08 11:56:36 +02:00
}
return EXIT_SUCCESS;
}