Click here to Skip to main content
15,896,408 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>

main()
{
      FILE *p;
      char ch,s[100];
      char r[100]="REN ";

      char u[]=".{21EC2020-3AEA-1069-A2DD-08002B30309D} ";
      char v[50];
      int choice,i;

      system("cls");
      p=fopen("D:\a.bat","w+");
      if(p==NULL)
      {
           printf("Error in opening the file a.c");
           exit(0);
      }


           printf("This software can convert your File/Folder to Control Panel and can Restore again.");
           printf("Enter the path of the file");
           fflush(stdin);
           gets(s);

           for(i=0;i<25;i++)
           fputs("echo This software is not responsible for any loss in data",p);

           printf("Enter choice :\n");
           printf("1.Protect Folder/File\n");
           printf("2.Unprotect folder/File\n");
           printf("3.Exit \n");
           scanf("%d",&choice);

           switch(choice){

                         case 1:
                         strcat(r,s);

                          printf("Enter new name of your folder/file");
                          fflush(stdin);
                           gets(v);
                            strcat(r," ");
                            strcat(r,v);
                              strcat(r,u);
                                break;

                         case 2:
                         strcat(r,s);
                         strcat(r,u);
                         printf("Enter new name of your folder/file");
                         fflush(stdin);
                         gets(v);
                         strcat(r,v);
                         break;

                         default:
                         fclose(p);
                         remove("D:\a.bat");

                         exit(0);
                      }

                      {
                      fputs(r,p);
                      for(i=0;i<25;i++)
                      fputs(" echo This software is not responsible for any loss in data ",p);
                      fputs("exit",p);
                      }

                      fclose(p);
                      system("C:\a.bat");
                      remove("C:\a.bat");
                      system("pause");

                      getch();
}
Posted
Comments
Mehdi Gholam 9-Jan-12 11:22am    
What is your error?
reikuxian 9-Jan-12 11:36am    
to be able to open and read a file in a directory..
i cant barely know the error because when i try to run the exe will come out after 1 sec..it will easily gone..
Chandrasekharan P 9-Jan-12 11:55am    
That tells us that its not your code. did you try to debug and find out where the it fails??
reikuxian 9-Jan-12 12:02pm    
YES!..i get from an internet then i fix the error then little by little the error become few..but afterwards i thought it has no error anymore yet i come up with that problem.. the .exe file will come up and then automatically vanished..
Richard MacCutchan 9-Jan-12 12:12pm    
Sorry, ignore my previous comment. Your filename strings are all wrong as they have unescaped backslash characters in them. Change "D:\a.bat" to "D:\\a.bat" (two backslash characters) and it may work better. Also you are using C: at the end of your program, which will fail, change C: to D:.

1 solution

C++
      char ch,s[100];
      char r[100]="REN ";
...
      char v[50];

You're using fixed size strings here! Your later operations, most notably strcat, are likely to write beyond the end of these arrays and cause undefined behaviour! Even if it works for simple tests, the later concatenations depend on the input folder name, and therefore the length of that name. Best way to avoid that would be using an actual string type, such as std::string.

C++
p=fopen("D:\a.bat","w+");

As has been pointed out in the comments, '\' is an escape character, and this line, as well as the others below, are not accessing the file you think it does - in fact it will likely be the reason for your program to crash, or fail at a later point. Replace all single '\' in your filenames with double '\\'. better yet, define a string variable to hold that filename instead of constantly repeating the actual name - and then mistyping it to "C:\a.bat" like you did towards the end.

C++
for(i=0;i<25;i++)
fputs("echo This software is not responsible for any loss in data",p);

... and later ...
C++
for(i=0;i<25;i++)
fputs(" echo This software is not responsible for any loss in data ",p);
fputs("exit",p);
}

Did you really mean to repeat that 50 times? Or write it at all for that matter?
C++
                         case 1:
...
                          printf("Enter new name of your folder/file");
... 
                         case 2:
...
                         printf("Enter new name of your folder/file");

Not a helpful comment you print there - you might want to change them to something meaningful - or at least different.
 
Share this answer
 
Comments
Richard MacCutchan 10-Jan-12 13:06pm    
Sound (visual) advice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900