1

I want to be able to delete a file using c++ remove functions but gets does not work in visual studio 2019 and using fget and gets_s ,the program runs but everytime it shows message that file not deleted as i proggrammed, The question is when it will delete .I tried every thing but it doesn't work. There would be solutions but i am a student so plz consider my problem.Here is my code :

#include <iostream>
#include <conio.h>
#include <stdio.h>
int main()
{
    int status;
    char file_name[59];
    cout << "enter name of file to delete:\n ";
    gets(file_name);
    status = remove(file_name);
    if (remove(file_name) == 0) //i wrote if(file_name==0); too but it doesn't work
        cout << "file deleted";
    else
        cout << "file not deleted";
}

I was looking for answers in stack over flow other similar questions but they were too professional to be understand by me plz tell me simple way to delete a file that user wants to delete.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 4
    You're calling `remove` twice. I'd never expect a successful message because either the file has already been removed, or you'll fail for the same reason the second time. – Stephen Newell Dec 15 '19 at 19:11
  • 3
    `gets` is so toxic that it was stricken from both C and C++ and its code was buried on unhallowed ground. [Do not use it](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). Consider using `std::getline` to read into a `std::string` and use `c_str` or `data` methods to get the `char` array from the `string` for `remove`. – user4581301 Dec 15 '19 at 19:11
  • 2
    `if (remove(file_name) == 0)` ===> `if (status == 0)` – WhozCraig Dec 15 '19 at 21:19
  • Thanks WhozCraig you did a great job,God bless you.It really solved my problem with using gets_s and using if(status==0).Thanks to stack over flow for this precious platform. – I Love Programming Dec 16 '19 at 08:22

0 Answers0