-
Notifications
You must be signed in to change notification settings - Fork 0
/
sayings.c
103 lines (73 loc) · 3.22 KB
/
sayings.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
// void scan(FILE*);
int main(){
char sayings[100][80], emptycharacter;
char saying[80], temp[80], query[20], newname[20], filename[20];
int userInput, i;
int numberOfSayings = 0;
printf("What's the name of the startup data file? ");
scanf("%s", filename);
//opens the file using fopen (FILE = type of variable *filepointer) - filepointer now points to the address of the given filename
FILE *filepointer = fopen(filename, "r");
if (filepointer == NULL) {
printf("File is not valid, cannot be read\n");
}
while (!feof(filepointer)) { //feof = checks if given parameter is currently at end of file
fgets(sayings[numberOfSayings], 80, filepointer); //includes newline character and automaticallly appends terminating null character
numberOfSayings++;
}
//menu of options
printf("Choose one of the following options:\n 1. Displaying all sayings currently in the database. \n 2. Enter a new saying into the database.\n 3. List sayings that contain the given word (substring) entered by the user.\n 4. Save all sayings in a new text file.\n 5. Quit the program.\n");
do { //ask for user input at least once, then go into switch statement
printf("What do you want to do now? ");
scanf("%d", &userInput);
switch (userInput) {
case 1:
//display all sayings currently in database
for (i = 0; i < numberOfSayings; i++) {
printf("%s", sayings[i]);
} //will print out sayings by row
break;
case 2:
//enter a new saying into the database
printf("Enter the new saying you want to add:\n");
// scanf("%s", temp);
scanf("%c", &emptycharacter); //capture newline input from keyboard
fgets(sayings[numberOfSayings], 80, stdin); //read input from standard input (string is at max 80 characters long) and put it into sayings array
numberOfSayings++;
break;
case 3:
//list sayings that contain a given word entered by user
printf("What's the word you're looking for?\n");
scanf("%s", query);
//look for search query in the sayings 2D array - find the first occurrence of query in sayings, ignore case
for (i=0; i < numberOfSayings; i++) {
if (strcasestr(sayings[i], query) != NULL) {
printf("%s", sayings[i]);
}
}
break;
case 4:
//take in user-input about file name
printf("What do you want the new file to be called?\n");
scanf("%s", newname);
FILE *newfilepointer = fopen(newname, "w+"); //creates file pointer, w+ creates an empty file and opens it for update (if file already exists)
//save sayings array to the new file
for(i = 0; i<numberOfSayings; i++){
fputs(sayings[i], newfilepointer);
}
break;
case 5:
userInput = -1;
break;
default:
printf("Please put in a valid input\n");
//re-prompt user for correct input
printf("Choose one of the following options:\n 1. Displaying all sayings currently in the database.\n 2. Enter a new saying into the database.\n 3. List sayings that contain the given word (substring) entered by the user.\n 4. Save all sayings in a new text file.\n 5. Quit the program.\n");
continue; //go back up to do loop and re-prompt for userinput
}
} while (userInput > 0);
return 0;
}