NO

Author Topic: deleting files  (Read 5692 times)

cd

  • Guest
deleting files
« on: February 09, 2010, 11:03:02 PM »
sup  ;) , i've got a problem with a function .
the function needs two files passwd and user .
this function should delet the two listed files below, but the functions just removes one file.
why ? any idea why?

ps: please keep in mind to get it runin there is alot of more code for it ... thats just a part of it ...

Code: [Select]


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

main(){

u_loeschen();

return 0;
}

int u_loeschen(void)
{
 
 FILE *fp;
 char datei[]= {"user.txt"};   
 char datei2[]= {"passwd.txt"};
 char antwort;
 
 printf(" do you want to delet the User? (Y)es /(N)o : \n");
 scanf("%s",&antwort);
 
 if(antwort == 'Y' || antwort == 'y')
 {
   remove(datei);
   printf(" userfile deleted!\n");
   remove(datei2);
   printf(" password files deleted!\n");
 
 }     
else
 {
  return 1;
 }
if (remove(datei)==0)
  {
      fprintf(stderr, "Error!! couldn't delete files!");
  }
return 0;   
 }

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: deleting files
« Reply #1 on: February 10, 2010, 08:09:25 AM »
I looked at your code in the debugger and found, that you defined for antwort the type char. In the scanf-function you read a string to that char. This overwrites the string datei2 and the filename of the second file is destroyed.

Use scanf("%c",&antwort); and all will work.
best regards
 Alex ;)

cd

  • Guest
Re: deleting files
« Reply #2 on: February 10, 2010, 05:06:53 PM »
I looked at your code in the debugger and found, that you defined for antwort the type char. In the scanf-function you read a string to that char. This overwrites the string datei2 and the filename of the second file is destroyed.

Use scanf("%c",&antwort); and all will work.

dammit,you made my day again didn't seen it... thanks again

[edit]

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. =)  


Code: [Select]
int u_loeschen(void)
{
 
 char datei[]= {"user.txt"};   
 char datei2[]= {"passwd.txt"};
 int antwort;
 int Y=JA;                                                  /* JA & NEIN are globales  for 1 and 0 */
 int y=JA;
 int N=NEIN;
 int n=NEIN;
 
 printf(" do you want to delet the User? (Y)es /(N)o : \n");
 scanf("%d",&antwort);
 
   if(antwort == 1)
     {
      remove(datei);
      printf(" User file deleted!\n");
      remove(datei2);
      printf(" Passwd file deleted!\n");
      }else{
            printf("Files are still there!!\n");
      }
      if(remove(datei)==0 && remove(datei2) == 0)
        fprintf(stderr, " Error coudln't delet the Files !");     
     
return 0;   
 }
« Last Edit: February 11, 2010, 01:01:27 AM by cd »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: deleting files
« Reply #3 on: February 11, 2010, 09:02:17 AM »
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.
best regards
 Alex ;)

cd

  • Guest
Re: deleting files
« Reply #4 on: February 12, 2010, 02:37:35 PM »
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 :
Code: [Select]

#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;  
 }
« Last Edit: February 12, 2010, 02:42:34 PM by cd »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: deleting files
« Reply #5 on: February 15, 2010, 08:46:22 AM »
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 :

int u_loeschen(void)
{
 .
 .
 char eingabe[2];
 .
 .
 scanf("%s",&eingabe);                             // copy and past error - here you need only eingabe
}

For the menus look in the help for kbhit(). Perhaps it is also an idea. ;)
best regards
 Alex ;)

cd

  • Guest
Re: deleting files
« Reply #6 on: February 15, 2010, 11:19:15 PM »
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 :

int u_loeschen(void)
{
 .
 .
 char eingabe[2];
 .
 .
 scanf("%s",&eingabe);                             // copy and past error - here you need only eingabe
}

For the menus look in the help for kbhit(). Perhaps it is also an idea. ;)

ty, may that made the trouble with the looping menue ... i wrote that little thing here 5 years ago ... but its funny to get back into the knowledge i had ... ^^  ... the kbhit function brought me to the clrscr() - function.

the kbhit () function is a good choice can i use it like this ? :

Code: [Select]
int x;

x= kbhit();

if(x == 0 )
  {
   while(JA)
     {
       switch
          {
           .
           .
           .
           }
      }
  }
 else {
            printf("Error\n");
         return 0;
        }




or like this :

Code: [Select]

int x;

x= kbhit();
   while(JA)
     {
       switch
          {
           .
           .
           .
           }
      }
if(x != 0 )
  {
     scanf("%d", &antwort);
  }


if not how is your idea to implement this function into the menue part ?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: deleting files
« Reply #7 on: February 16, 2010, 07:59:46 AM »
Another generic function to use and dummy example.
Code: [Select]
#include <stdio.h>
#include <conio.h>

int main(int argc, char **argv)
{
int ch;

do
{
//ch = getchar(); // needs enter to select
ch = _getch();
switch(ch)
{
case 'a': printf("menu a\n"); break;
case 's': printf("menu s\n"); break;
}
//printf("%c\n", ch);
} while (ch != 'x'); // x to exit

return 0;
}
May the source be with you

cd

  • Guest
Re: deleting files
« Reply #8 on: February 16, 2010, 11:36:35 PM »
Another generic function to use and dummy example.
Code: [Select]
#include <stdio.h>
#include <conio.h>

int main(int argc, char **argv)
{
int ch;

do
{
//ch = getchar(); // needs enter to select
ch = _getch();
switch(ch)
{
case 'a': printf("menu a\n"); break;
case 's': printf("menu s\n"); break;
}
//printf("%c\n", ch);
} while (ch != 'x'); // x to exit

return 0;
}

ty for your input , but it's just a different choice of what already exsist.
the one thing wich i was looking for to make sure that there isn't a buffered string or integer value when it returns to the menue.to work with strings is an idea , but it doesn't make sure that there isn't a buffered string or integer.
 the case that there is an  buffered string might not fix the issue of the looping menue.
i guess it will end up with a runtime error with a buffered string instead of solving my minded situation.