Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StudentFunction.h"
- void CreateUser(struct User* userData, int numberOfUsers)
- {
- printf("Choose your username ( max 64 characters and no space ): ");
- scanf("%s", userData->username);
- printf("Choose your password ( max 64 characters and no space ): ");
- scanf("%s", userData->password);
- printf("Your username is: %s \nYour password is: %s ", userData->username, userData->password);
- srand((unsigned)time(NULL));
- srand((unsigned)time(NULL));
- for (int j = 0; j < numberOfUsers; j++)
- {
- int keyLo = rand() % 6 + 3;
- for (int i = 0; i < keyLo; i++)
- {
- userData[j].key[i] = rand() % 94 + 33;
- }
- userData[j].key[keyLo] = '\0';
- }
- }
- void encryped(char text[], char encryptdText[], char key[])
- {
- int i = 0;
- int j = 0;
- int lengthOfText = strlen(text);
- while (i < lengthOfText)
- {
- if (j == strlen(key))
- {
- j = 0;
- }
- if (text[i] <= 32 || text[i] >= 126 || text[i] =='n'|| text[i] == '\0')
- {
- encryptdText[i] = text[i];
- }
- else
- {
- encryptdText[i] = (text[i] - 32 + key[j] - 32) % 95 + 32;
- j++;
- }
- i++;
- }
- }
- int LogIn(struct User* userData, int numberOfUsers)
- {
- char username[64];
- char password[64];
- int a = 1;
- int check = 0;
- int position;
- for (int i = 0; i < numberOfUsers; i++)
- {
- printf("%s\t%s\n", userData[i].username, userData[i].password);
- }
- while (a == 1)
- {
- printf("Enter your Username: \n");
- scanf("%s", &username);
- getchar();
- printf("Enter your Password: \n");
- scanf("%s", &password);
- getchar();
- for (int i = 0; i < numberOfUsers; i++)
- {
- if ((strcmp(username, userData[i].username) != 0) || strcmp(password, userData[i].password) != 0)
- {
- }
- else
- {
- check = 1; // checkar om dom är lika varanda
- position = i; //vilken postion användaren är på
- i = numberOfUsers;
- a = 0;
- printf("Login correct!");
- }
- }
- }
- return position;
- }
- void StartMenu()
- {
- printf("\n MENU \n");
- printf("----------------\n");
- printf("1. Log In. \n");
- printf("2. Create user.\n");
- printf("3. Quit program. \n");
- }
- void MainMenu(struct User* userData, int userID, int numberOfUsers)
- {
- int choice = 1;
- char textIn[1000];
- char newfilename[1000];
- FILE* pFileText;
- pFileText = fopen("text.txt", "a+");
- struct User pUser;
- char fileSaveChoice;
- char fileConext[1000]; //BYTTTTTNAMNNNN!!!
- char newPass[64];
- char encryptdText[1000];
- while (choice != -1)
- {
- /*char fileChoice;*/
- char fileChoice[1000];
- printf("\n\n_______|MENU|__________\n");
- printf("\n");
- printf("1. Input your text \n");
- printf("2. Save your text to a new file \n");
- printf("3. Save your text in an already existing file \n");
- printf("4. Choose a file to read and present it's content (uncrypted) \n");
- printf("5. Change your password\n");
- printf("6. log out \n");
- printf("7. exit program \n");
- scanf("%d", &choice);
- getchar();
- switch (choice)
- {
- case 1:
- printf("Write your text: ");
- fgets(textIn, 1000, stdin);
- printf("%s", textIn);
- printf("do you want to save your text in the existing document? (y/n)?: ");
- scanf("%c", &fileSaveChoice);
- if (fileSaveChoice == 'y')
- {
- fputs(textIn, pFileText);
- fclose(pFileText);
- }
- else
- {
- printf("\nPlease chose a new option\n");
- choice = 0;
- }
- break;
- case 2:
- printf("\n%s\n", textIn); //remove!!!//
- printf("chose a new name for the file");
- scanf("%s", newfilename);
- FILE *pNewFile;
- pNewFile = fopen(newfilename, "w");
- encryped(textIn, encryptdText, userData->key);
- fputs(textIn, pNewFile);
- fclose(pNewFile);
- break;
- case 3:
- printf("save your text to an already existing file \n");
- printf("chose an existing file: ");
- scanf("%s", pUser.fileName);
- getchar();
- FILE * pExistingfile;
- pExistingfile = fopen(pUser.fileName, "a+");
- fputs(textIn, pExistingfile);
- fclose(pExistingfile);
- break;
- case 4:
- printf("what file would you like to choose?: ");
- scanf("%s", fileChoice);
- FILE * fileChoicePointer;
- fileChoicePointer = fopen(fileChoice, "r");
- while (fgets(fileConext, sizeof(fileConext), fileChoicePointer) != NULL)
- {
- printf("\n");
- fputs(fileConext, stdout);
- }
- fclose(fileChoicePointer);
- break;
- case 5:
- printf("Write your new password: ");
- scanf("%s", newPass);
- strcpy(userData[userID].password, newPass);
- printf("%s", userData[userID].password);
- FILE* outFile;
- outFile = fopen("User.txt", "r+");
- fseek(outFile, 3, SEEK_SET);
- for (int i = 0; i < numberOfUsers; i++)
- {
- fprintf(outFile, "%s\t%s\n", userData[i].username, userData[i].password);
- }
- fclose(outFile);
- break;
- case 6:
- printf("\nlogging out\n");
- StartMenu();
- choice = -1;
- break;
- case 7:
- printf("exit");
- exit(1);
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement