summaryrefslogtreecommitdiff
path: root/utils.c
blob: c08990c1429dadd3b3d38f2b6b40af7c2f951d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include "utils.h"

int checkIfFileExists(char *path)
{
	return access(path, F_OK);
}

int lineCounter(FILE *dictionary)
{
	int words = 0;
	if ( dictionary != NULL )
	{
		char line[256];
		while ( !feof(dictionary) )
		{
			fgets(line, sizeof(line), dictionary);
			words++;
		}
		words--;
		rewind(dictionary);
	}

	return words;
}

char* getLine(FILE *dictionary, int lineNumber)
{
	char *line = (char *) calloc(256, sizeof(char));
	if ( dictionary != NULL )
	{
		for ( int i = 0; (!feof(dictionary) && i != lineNumber - 1); i++)
		{
			fgets(line, sizeof(line), dictionary);
		}
		rewind(dictionary);
	}
	return line;
}

int getRandomNumber(int min, int max)
{
	srand(time(0));
	return ((rand() % (max - min + 1)) + 1);
}