or may not, to change the input into a c like you said worked but, the menue of the little programm were listed 2 times ...
so i had to solve it to change the input into a int value and the problem is solved now. =)
If you that you solved your problem with the code you posted, I am very surprised.
1.) Your scanf("%d", &antwort) need as input a number and not how you pretend a character like 'y', 'Y', 'n' or 'N'. And it helps you nothing that you local integers with the same name.
2.) If your input is '0', so that the files should not be deleted the program runnes in the else code - correct. But behind the if(remove(datei)... will delete your files.
true. after deleting the last if-line the programm started to loop the menue.
after checking it how to fix it ... i focused the deleting function and checked the output of each variable wich is decleared in the deleting function.
I made sure that the input is saved in one character-array to make sure that the string or charackter will be saved , not lost or overwrites another variable.
i changed it again and hopefully it will work finally ... i checked it a couple times and i guess the work is done with it now ... .
ps: nothing is crypted just a try for playin around with pointer , functions and getting used to the file handling
here the complete source of it :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define Groesse 10
#define MAX 10
#define JA 1
#define NEIN 0
char *u_name;
char *u_passwd;
int i=0 ;
int menue(void);
int u_anlegen(void);
int u_loeschen(void);
int ausgabe(void);
int main (void)
{
while(JA)
{
switch(menue())
{
case 1:
{
u_anlegen();
continue;
}
case 2:
{
ausgabe();
continue;
}
case 3:
{
u_loeschen();
continue;
}
case 4:
{
puts("EXIT PROGRAMM SUCCESSFULL!.\n");
exit (JA);
}
default:
{
puts("theres nothing to deal with! try it again ! .");
continue;
}
}
return 0;
}
}
int menue(void)
{
fflush(stdin); /*it should refresh the input stream to make sure it isn't runing with buffered values*/
int antwort;
printf("\n\n\n");
puts("\n choose 1 for creating a username.\n");
puts("\n choose 2 for displaying the username and the password.\n");
puts("\n choose 3 for deleting the files.\n");
puts("\n choose 4 for EXIT .\n");
scanf("%d", &antwort);
return antwort;
}
/* function to create a user and a password for it (uncrypted) */
int u_anlegen(void)
{
FILE *fp;
extern char *u_name;
extern char *u_passwd;
extern int i;
/*opening the file to write the username into the userstxt
* and dynamic memory building if might be not enough memory free
* for it or the file doesn't exsist
*/
/* USERNAME PART */
fp=fopen("user.txt","w+");
u_name=(char*)calloc(MAX, sizeof(int));
if (u_name == NULL )
{
fprintf(stderr,"NOT ENOUGH MEMORY!\n");
exit(JA);
}
else
{
printf(" choose a username :\n");
scanf("%s",&*u_name);
if ((fwrite(u_name,sizeof(int),MAX, fp))!= MAX)
{
fprintf(stderr," Error ! Username isn't set up! \n");
exit(JA);
}
}
rewind(fp);
fclose(fp);
/*PASSWORD PART */
fp=fopen("passwd.txt", "w+");
u_passwd=(char*)calloc(MAX, sizeof(int));
if ( u_passwd == NULL)
{
fprintf(stderr, "\n Password not ready! not enough memory !\n");
return 0;
}
else
{
printf(" choose a password:\n");
scanf("%s" ,&*u_passwd);
if ((fwrite(u_passwd,sizeof(int),MAX, fp))!= MAX)
{
fprintf(stderr," Error ! with saving the password\n");
return 0;
}
rewind(fp);
fclose(fp);
}
}
/* function for displaying the user and the password */
/* Username and Password can't be bigger than 8 letters*/
int ausgabe(void)
{
FILE *fp;
char u_name[MAX];
char u_passwd[MAX];
if((fp= fopen("user.txt","r"))== NULL)
{
fprintf(stderr,"Error, couldn't read a username!\n");
}
while (!feof(fp))
{
fgets(u_name,8,fp);
printf("Users Name is : %s",u_name);
break;
}
printf("\n");
fclose(fp);
if ( (fp=fopen("passwd.txt","r"))== NULL)
{
fprintf(stderr,"Error couldn't read the password!\n");
}
while (!feof(fp))
{
fgets(u_passwd,8,fp);
printf("the choosen password for the user : %s",u_passwd);
break;
}
printf("\n");
fclose(fp);
}
/* function to delet user & passwd file */
/* files gonna be deleted completely */
int u_loeschen(void)
{
char datei[]= {"user.txt"};
char datei2[]= {"passwd.txt"};
char eingabe[2];
char check1[3]={'Y','y'}; /*char arry for checking the falue of eingabe*/
char *ptr;
/*pointer for the remove function and for checking the input(eingabe)*/
ptr=eingabe;
printf(" do you want to delet the User? (Y)es /(N)o : \n");
scanf("%s",&eingabe);
if( *ptr == check1[0] || *ptr == check1[1])
{
ptr=datei;
remove(ptr);
printf(" User file deleted!\n");
ptr=datei2;
remove(ptr);
printf(" Passwd file deleted!\n");
}else{
printf("Files are still there!!\n");
}
return 0;
}