Files
TD3_DEV51_vaisse_kara-mosr/getfuncs.c
2025-10-08 12:35:01 +02:00

80 lines
1.2 KiB
C

#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);
char read[8];
unsigned int counter = 0;
int char_size = (int) sizeof(char);
int lign=0;
int offset=0;
/*prog*/
printf("%d\n", random);
stream = fopen("wordbank.txt", "r");
if (stream == NULL){
return -1;
}
for(lign=0; lign<random; lign++){
while(*read!='\n'){
fread(read, sizeof(char), 1, stream);
offset++;
}
*read=' ';
}
while(*read!='\n'){
fread(read, sizeof(char), 1, stream);
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){
printf("%s", fullword);
}
return EXIT_SUCCESS;
}