Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning (disable: 4996)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "Functions2.h"
- #include "Functions.c"
- char content[100];
- int answer = 0, length = 0, found = 0, users = 0, choice3 = 0, k = 1;
- long size = 0;
- char test[10];
- int main() {
- /*User Struct which is used in order to create a new profile with a name, password, key and potential new
- files (Created after the login)*/
- typedef struct User {
- char name[16];
- char password[16];
- char key[8];
- char files[8][16];
- };
- struct User* User2 = NULL;
- struct User* currentUser = NULL;
- //Loading
- file = fopen("Profile", "rb");
- if (file != NULL) {
- fseek(file, 0, SEEK_END);
- size = ftell(file);
- length = size / sizeof(struct User);
- User2 = (struct User*)malloc(size);
- rewind(file);
- fread(User2, sizeof(struct User), length, file);
- users = length;
- fclose(file);
- }
- else {
- printf("Error loading data!\n");
- }
- printf("Hello! Welcome to Final Project\n\n");
- while (1) {
- if (choice == 0) {
- printf("Press (1) to login\n");
- printf("Press (2) to create a new account\n");
- printf("Your choice: ");
- scanf("%d", &answer);
- getchar();
- printf("\n");
- if (answer == 1) {
- char userAttempt[16];
- char userPassword[16];
- printf("Insert Username: "); scanf("%s", userAttempt);
- getchar();
- printf("\nInsert Password: "); scanf("%s", userPassword);
- getchar();
- for (int z = 0; z < 2; z++) {
- if (strcmp(userAttempt, User2[z].name) == 0 && strcmp(userPassword, User2[z].password) == 0) {
- printf("\nLogin success!");
- currentUser = &User2[z];
- choice = 1;
- }
- }
- }
- else if (answer == 2) {
- file = fopen("Profile", "wb");
- //If not found
- //Read/Save.
- char name[16];
- char password[16];
- printf("Insert Username: "); scanf("%s", name);
- getchar();
- printf("Insert Password: "); scanf("%s", password);
- getchar();
- printf("\n");
- //Create a Profile
- if (file == NULL) {
- User2 = (struct User*)malloc(sizeof(struct User));
- }
- else {
- User2 = (struct User*)realloc(User2, sizeof(struct User)*(users + 1));
- }
- strcpy(User2[users].name, name);
- strcpy(User2[users].password, password);
- users++;
- //Save.
- fwrite(User2, sizeof(struct User), users, file);
- //Close
- choice = 1;
- fclose(file);
- }
- else if (answer != 1, 2) {
- printf("\nPlease use the the number 1 and 2 to proceed.\n");
- getchar();
- }
- }
- else if (currentUser != NULL || choice == 1) {
- printf("\n\n What do you wish to do?");
- printf("\n 1) Insert Text");
- printf("\n 2) Save data on an existing file");
- printf("\n 3) Read a file");
- printf("\n 4) Change password");
- printf("\n 5) Log out");
- printf("\n 6) Close Program");
- printf("\n\nYour Choice: ");
- int choice2;
- scanf("%d", &choice2);
- getchar();
- switch (choice2) {
- case 1:
- printFile();
- break;
- case 2:
- printf("What file do you wish to save data on?\n");
- scanf("%s", &openFile);
- getchar();
- file = fopen(openFile, "r+");
- if (file == NULL) {
- printf("Could not open file.");
- getchar();
- return 0;
- } else {
- fscanf(file, "%s", content);
- }
- fclose(file);
- break;
- case 3:
- printf("\nWhat file do you wish to read?\n");
- printf("File: ");
- scanf("%s", &openFile);
- getchar();
- file = fopen(openFile, "r");
- if (file != NULL) {
- fseek(file, 0, SEEK_END);
- int lengthSeek = ftell(file);
- rewind(file);
- char *c = (char*)malloc(lengthSeek);
- fread(c, lengthSeek, 1, file);
- fclose(file);
- for (int u = 0; u < lengthSeek; u++) {
- printf("%c", c[u]);
- }
- free(c);
- }
- else if (file == NULL) {
- printf("File doesn't exist.. Try creating it first!");
- choice = 1;
- }
- break;
- case 4:
- printf("Insert new password here: ");
- scanf("%s", currentUser->password);
- getchar();
- break;
- case 5:
- choice = 0;
- break;
- case 6:
- return 0;
- break;
- default:
- printf("Please use the numbers 1 to 7!");
- choice = 1;
- break;
- getchar();
- }
- }
- }
- getchar();
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement