-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.c
More file actions
61 lines (56 loc) · 1.46 KB
/
student.c
File metadata and controls
61 lines (56 loc) · 1.46 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#define FIFO_WT "fifo_wt" // the fifo that the student reads from
#define FIFO_RT "fifo_rt" // the fifo that the student writes to
#define STR_LEN 100
int main(int argc, char *argv[])
{
/* 1. open reader and writer fifo recpectively*/
char word[STR_LEN];
FILE *fdw;
FILE *fdr;
/*write*/
if (!(fdr = fopen(FIFO_RT, "w")))
{
perror("cannot open fifo file for w");
exit(EXIT_FAILURE);
}
// create a fifo for the student to read from
if (mkfifo(FIFO_WT, 0666 | O_RDONLY) == -1 && errno != EEXIST)
{
perror("cannot create fifo file");
exit(EXIT_FAILURE);
}
/*read*/
if (!(fdw = fopen(FIFO_WT, "r")))
{
perror("cannot open fifo file for r");
exit(EXIT_FAILURE);
}
/* 2. recive strings from user until receiving "exit" */
while (1)
{
printf("Enter English Word: ");
if (fgets(word, STR_LEN, stdin) != NULL)
{
fprintf(fdr, " %s\n", word);
fflush(fdr); // <== important
fscanf(fdw, " %s", word);
fflush(fdw);
printf("The word in German: %s\n", word);
if (strcmp(word, "Bye") == 0)
break;
}
}
fclose(fdw);
fclose(fdr);
unlink(FIFO_RT);
unlink(FIFO_WT);
return EXIT_SUCCESS;
}