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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include "resource.h"
#define MAX 256
#define MAXPASS 20
const char cpassnam[] = "pass.txt";
void inputData();
void retrieveData();
void modifyData();
void viewInfo();
void deleteData();
struct Info
{
char name[50];
char lastname[50];
char city[50];
char country[50];
char tel[20];
}info;
void cgetstr(const char* prm, char* str, int csz)
{
str[0] = 0;
while(printf("%s", prm) && (!fgets(str, csz, stdin) || *str == 0))
{
puts("\n Invalid input\n");
}
}
...
...
int main()
{
...
}
...
...
void modifyData()
{
int temp, status;
unsigned options;
do
{
system("cls");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n\tSection Modify info\n\t-------------------");
printf("\n\n\tChoose an option:\n\n");
printf("\n 1. Modify info");
printf("\n 2. Back to main menu");
printf("\n\n\n Select: ");
status = scanf("%d", &options);
while((temp = getchar()) != EOF && temp != '\n');
if(!status)
{
printf("\n\n Invalid option.. Choose 1 or 2\a");
Sleep(2000);
system("cls");
options = 0;
continue;
}
else if(options == 1)
{
BOOL got = FALSE;
char pass[MAXPASS] = {0};
FILE* fs = fopen(cpassnam, "r+");
if(fs != NULL)
{
got = (fgets(pass, MAXPASS, fs) != NULL && strlen(pass) > 0);
}
if(got)
{
char newpass[MAXPASS] = {0};
system("cls");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n\tSection Modify info\n\t-------------------");
cgetstr("\n\n\n Enter existing password: ", newpass, MAXPASS);
const int l = strlen(pass) - 2;
if(pass[l] == '\n')
{
pass[l] = 0;
}
if(strcmp(pass, newpass) == 0)
{
FILE *fptr1, *fptr2;
int lNo, lineCtr = 0;
char str[MAX], fName[MAX];
char newLn[MAX], temp[] = "temp.txt";
printf("\n\n\n\n\t\t\t Access Granted\n");
Sleep(1500);
system("cls;");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n Replace old info");
printf("\n\n Input the file name to be opened : ");
fgets(fName, MAX, stdin);
fName[strlen(fName) - 1] = '\0';
fptr1 = fopen(fName, "r");
while(1)
{
if(!fptr1)
{
printf("\n\n Unable to open the input file\n\a");
Sleep(1000);
printf("\n\n ..returning to menu");
Sleep(1500);
break;
}
fptr2 = fopen(temp, "w");
if(!fptr2)
{
printf("Unable to open a temporary file to write\n");
Sleep(2000);
fclose(fptr1);
}
printf("\n\n Update the old info:");
printf("\n\n\n\tName: ");
scanf("%49s", info.name);
printf("\n\n\tLast name: ");
scanf("%49s", info.lastname);
printf("\n\n\tCity: ");
scanf("%49s", info.city);
printf("\n\n\tCountry: ");
scanf("%49s", info.country);
printf("\n\n\tPhone: ");
scanf("%19s", info.tel);
fprintf(fptr2, "%s %s %s %s %s\n",
info.name, info.lastname, info.city, info.country, info.tel);
fgets(newLn, MAX, stdin);
printf("\n\n Input the line number you want to replace : ");
scanf("%d", &lNo);
lNo++;
while (!feof(fptr1))
{
strcpy(str, "\0");
fgets(str, MAX, fptr1);
if (!feof(fptr1))
{
lineCtr++;
if (lineCtr != lNo)
{
fprintf(fptr2, "%s", str);
}
else
{
fprintf(fptr2, "%s", newLn);
}
}
}
fclose(fptr1);
fclose(fptr2);
remove(fName);
rename(temp, fName);
printf("\n\n\n\n\t\t\t Replacement successfully..!");
Sleep(2000);
break;
}
}
else
{
puts("\n\n Wrong password\a");
Sleep(1000);
printf("\n\n\n\n\t\t\t Access Denied\n");
Sleep(1500);
}
}
else
{
if(fs != NULL)
{
fclose(fs);
}
fs = fopen(cpassnam, "w");
system("cls");
printf("\n\t\t\t\tData Base\n\t\t\t\t---------");
printf("\n\n\n\n To modify information, requires a password.");
cgetstr("\n\n Register new password: ", pass, MAXPASS);
fprintf(fs, "%s\n", pass);
printf("\n\n Password saved to data base");
Sleep(2000);
system("cls");
}
fclose(fs);
}
else if(options == 2)
{
printf("\n\n\n ..returning to main menu");
Sleep(2000);
system("cls");
}
else
{
printf("\n\n Invalid option.. Choose 1 or 2\a");
Sleep(2000);
system("cls");
}
}while(options != 2);
}
|