random quote done
This commit is contained in:
parent
19a02a84ae
commit
8d7ff35875
@ -4,20 +4,54 @@
|
|||||||
|
|
||||||
#include "quote.h"
|
#include "quote.h"
|
||||||
|
|
||||||
void getRandomQuote(char quote[1024]) {
|
void getRandomQuote(char quote[2048]) {
|
||||||
// TODO: true random between 0 and number of lines in file
|
int lineCount;
|
||||||
int rand = 0;
|
long line;
|
||||||
getQuote(rand, quote);
|
|
||||||
|
if ((lineCount = getFileLineCount(QUOTES_FILE)) == -1) {
|
||||||
|
puts("file does not exist");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
line = random() % lineCount;
|
||||||
|
|
||||||
|
getQuote(line, quote);
|
||||||
}
|
}
|
||||||
|
|
||||||
void getQuote(int line, char quote[1024]) {
|
void getQuote(long line, char quote[2048]) {
|
||||||
FILE* list;
|
FILE* list;
|
||||||
|
|
||||||
// TODO: get specified x line
|
list = fopen(QUOTES_FILE, "r");
|
||||||
|
|
||||||
list = fopen("quotes.txt", "r");
|
/*skip lines*/
|
||||||
|
while (line > 0) {
|
||||||
fgets(quote, 1024, list);
|
if (fgetc(list) == '\n') {
|
||||||
|
line--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fgets(quote, 2048, list);
|
||||||
|
quote[strlen(quote) - 1] = '\0';
|
||||||
|
|
||||||
fclose(list);
|
fclose(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getFileLineCount(char *filename) {
|
||||||
|
char c;
|
||||||
|
int lineCount = 0;
|
||||||
|
FILE* quotesFile = fopen(filename, "r");
|
||||||
|
|
||||||
|
if (quotesFile == NULL) {
|
||||||
|
printf("no quote file found at \"%s\"\n", filename);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((c = (char) fgetc(quotesFile)) != EOF) {
|
||||||
|
if (c == '\n') {
|
||||||
|
lineCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(quotesFile);
|
||||||
|
|
||||||
|
return lineCount;
|
||||||
|
}
|
||||||
|
|||||||
@ -6,11 +6,17 @@
|
|||||||
#define QOTD_QUOTE_H
|
#define QOTD_QUOTE_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <bits/types/FILE.h>
|
#include <bits/types/FILE.h>
|
||||||
#include <bits/types/struct_FILE.h>
|
#include <bits/types/struct_FILE.h>
|
||||||
|
|
||||||
void getRandomQuote(char quote[1024]);
|
#define QUOTES_FILE "./quotes.txt"
|
||||||
|
|
||||||
void getQuote(int line, char quote[1024]);
|
void getRandomQuote(char quote[2048]);
|
||||||
|
|
||||||
|
void getQuote(long line, char quote[2048]);
|
||||||
|
|
||||||
|
int getFileLineCount(char* filename);
|
||||||
|
|
||||||
#endif //QOTD_QUOTE_H
|
#endif //QOTD_QUOTE_H
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user