-
Notifications
You must be signed in to change notification settings - Fork 0
/
Msg.c
38 lines (31 loc) · 1.07 KB
/
Msg.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
#include "main.h"
/**
* collectvisitormessage - this function collects visitor's message
* Return: void
*/
void collectVisitorMessage()
{
char visitorName[MAX_NAME_LENGTH];
char recipient[MAX_NAME_LENGTH];
char message[MAX_MESSAGE_LENGTH];
char filePath[MAX_PATH_LENGTH];
getchar();
printf("Please enter your name: \n");
fgets(visitorName, MAX_NAME_LENGTH, stdin);
visitorName[strcspn(visitorName, "\n")] = '\0';
printf("Please enter the recipient's name: \n");
fgets(recipient, MAX_NAME_LENGTH, stdin);
recipient[strcspn(recipient, "\n")] = '\0';
printf("Please enter your message (max 255 characters):\n");
fgets(message, MAX_MESSAGE_LENGTH, stdin);
message[strcspn(message, "\n")] = '\0';
sprintf(filePath, "messages/%s_message.txt", visitorName);
FILE *fp = fopen(filePath, "w");
if (fp == NULL) {
printf("Error: unable to open messages file for writing.\n");
return;
}
fprintf(fp, "To:\n %s\n\n%s", recipient, message);
fclose(fp);
printf("Message successfully passed!!\n");
}