Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Dawid Mocek
- Zadanie 8
- Napisz program, który umożliwia automatyczne generowanie poczty elektronicznej. Program dostaje jako wywołanie dwa parametry:
- Plik z adresatami
- Plik z treścią wiadomości
- W pliku z adresatami każda linia ma następujący format:
- Imie Nazwisko adres@email
- Przykładowy plik z wiadomością:
- Drogi [KTO]
- Właśnie wygrałeś 100 000 EURO. Odpisz proszę, a ustalimy sposób doręczenie pieniędzy.
- Całuje,
- Ka.
- Zadaniem programu, jest stworzenie osobnego pliku z kopią wiadomości dla każdego adresata. Program powinien podmienić każde wystąpienie [KTO] na imię i nazwisko z pliku z adresatami i zapisać nową wiadomość w pliku o nazwie odpowiadającej emailowi.
- */
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "conio.h"
- const int NAME_LEN = 255;
- const int SURNAME_LEN = 255;
- // RFC 5321 max email len.
- const int EMAIL_LEN = 254;
- struct desc
- {
- char name[NAME_LEN];
- char surname[SURNAME_LEN];
- char email[EMAIL_LEN];
- };
- typedef FILE* _file;
- /* Replace function */
- char *replace(char *mesg, char *from, char *to)
- {
- static char buff[4096];
- char *ch;
- //strstr - returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
- if (!(ch = strstr(mesg, from)))
- return mesg;
- strncpy(buff, mesg, ch - mesg);
- buff[ch - mesg] = 0;
- sprintf(buff + (ch - mesg), "%s%s", to, ch + strlen(from));
- return buff;
- }
- int main(int argc, char **argv)
- {
- char emails_file_path[255];
- char mesg_file_path[255];
- char *result_dir_path = "D:\\emails\\";
- char line[254+255+255];
- char *mesg_source = NULL;
- if(argc-1 != 2)
- {
- printf("Need 2 params\n");
- getchar();
- exit(1);
- }
- strcpy(emails_file_path, argv[1]);
- strcpy(mesg_file_path, argv[2]);
- // file with emails
- _file p_emails_file = fopen(emails_file_path, "r");
- // file with mesg
- _file p_mesg_file = fopen(mesg_file_path, "r");
- if(p_emails_file == NULL)
- {
- printf("Error opening Email DB file");
- exit(1);
- }
- if(p_mesg_file == NULL)
- {
- printf("Error opening mesg DB file");
- exit(1);
- }
- /* Read entire mesg content */
- char *tmp = NULL;
- size_t bufSiz = 0;
- char inputBuf[256];
- while (fgets(inputBuf, sizeof inputBuf, p_mesg_file) != NULL)
- {
- tmp = (char*)realloc(mesg_source, bufSiz + strlen(inputBuf) + 1);
- if (tmp)
- {
- mesg_source = tmp;
- mesg_source[bufSiz] = 0;
- strcat(mesg_source, inputBuf);
- bufSiz += strlen(inputBuf) + 1;
- }
- else
- {
- printf("Error: allocated memory\n");
- free(mesg_source);
- mesg_source = NULL;
- break;
- }
- }
- /* Analyze Email File */
- while(fgets(line, 254, p_emails_file) != NULL)
- {
- // trim \r\n
- char* line_trim = strtok(line, "\r\n");
- // split string
- char* token[255];
- int cnt = 0;
- token[0] = strtok(line_trim, " ");
- // Tricky
- while (token[cnt] != NULL)
- {
- cnt++;
- token[cnt] = strtok (NULL, " ");
- }
- desc* field = (desc*)malloc(sizeof(desc));
- for (int j = 0; j <= cnt -1 ; j++)
- {
- //Create basic structure
- strcpy(field->name, token[0]);
- strcpy(field->surname, token[1]);
- strcpy(field->email, token[2]);
- }
- // Create filename: path + email + .txt(4)
- char *filename = (char*)malloc(strlen(result_dir_path) + strlen(field->email) + 4);
- strcpy(filename, result_dir_path);
- strcat(filename, field->email);
- strcat(filename, ".txt");
- // Open result file for given mail a+
- _file p_res = fopen(filename, "a+");
- if(p_res == NULL)
- {
- printf("Cannot open file: %s\n", filename);
- exit(1);
- }
- // 1 = whitespace
- char *name_and_surname = (char*)malloc(strlen(field->name) + 2 + strlen(field->surname));
- strcpy(name_and_surname, field->name);
- strcat(name_and_surname, " ");
- strcat(name_and_surname, field->surname);
- //replace [KTO]
- char* new_mesg = replace(mesg_source, "[KTO]", name_and_surname);
- //write
- //fprintf(p_res, mesg_source);
- fprintf(p_res, new_mesg);
- //Close
- fclose(p_res);
- free(name_and_surname);
- free(field);
- }
- fclose(p_emails_file);
- fclose(p_mesg_file);
- free(mesg_source);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement