Delete file in cpp

C++ remove()

The remove() function in C++ deletes a specified file. It is defined in the cstdio header file.

Example

#include #include using namespace std; int main() < char filename[] = "program.cpp"; 
// remove the file "program.cpp" int result = remove(filename);
cout // Output: -1

remove() Syntax

The syntax of the remove() function is:

remove(const char* filename);

remove() Parameters

The remove() function takes the following parameter:

  • filename — pointer to the C-string containing the name of the file along with the path to delete

Note: Variables of the C++ string class cannot be used as parameters for remove() .

remove() Return Value

The remove() function returns:

remove() Prototype

The prototype of remove() as defined in the cstdio header file is:

int remove(const char* filename);

Delete Opened Files with remove()

In case the file to be deleted is opened by a process, the behaviour of remove() function is implementation-defined:

  • POSIX systems — If the name was the last link to a file, but any processes still have the file open, the file will remain in existence until the last running process closes the file.
  • Windows — The file won’t be allowed to be deleted if it remains open by any process.
Читайте также:  Программа нельзя открыть файл html

Example: C++ remove()

#include #include using namespace std; int main() < char filename[] = "C:\\Users\\file.txt"; 
// deletes the file if it exists int result = remove(filename);
// check if file has been deleted successfully if (result != 0) < // print error message cerr else < cout return 0; >

Источник

Remove a File in C++

Remove a File in C++

  1. Delete or Remove a File in C++
  2. Conclusion

A file is a source of storing sequential data permanently in some permanent storage device like a hard drive, USB, memory card, etc.

The data in the file is not volatile like the output generated by a computer program in the Integrated Development Environment (IDE). But, it’s permanent, and the user can retrieve and utilize it in the long term.

Some of the operations that we can perform in C++ for file handling are reading, creating, writing, updating, and deleting a file.

Delete or Remove a File in C++

Before deleting a file, first, make sure the file exists. In C++, to delete a file, you will need the file’s complete path.

C++ provides us the remove() function of the header file stdio.h in order to delete a file. This function requires the path of the file to delete as a parameter.

This return 0 (FALSE) is the file deleted successfully; otherwise, a non-zero value (TRUE).

remove(path\filename.Extention) remove(E:\Article writing\MS2\delete a file c++.en) 

If you want to delete a file inside your file handling program’s directory, you only need to enter the file name as the path of the file. Otherwise, you need to specify the complete path of the file manually.

Disclaimer: Please consider that if you delete a file using remove() , the file will not be moved to the Recycle Bin to restore it. Instead, it will be deleted permanently.

#include  int main()   // provide the path of the file to delete.  if (remove("E:\Article writing\MS2\delete a file in c++") == 0)   printf("The file is deleted successfully.");  >  else   printf("The file is not deleted.");  >  return 0; > 
The file is deleted successfully. 

In this example, we compared the return value of remove() with 0 . If this condition is true, we can say that the file is successfully deleted; otherwise, the file is not deleted.

Conclusion

We have seen that we can delete an existing file with the remove() function of the stdio.h header file. This function requires the file path as a parameter along with filename and dot extension.

And the return value is 0 (zero) in case of successful deletion; otherwise, a non-zero value.

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

Related Article — C++ File

Источник

remove

Deletes the file whose name is specified in filename.

This is an operation performed directly on a file identified by its filename; No streams are involved in the operation.

Proper file access shall be available.

Parameters

filename C string containing the name of the file to be deleted.
Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).

Return value

If the file is successfully deleted, a zero value is returned.
On failure, a nonzero value is returned.
On most library implementations, the errno variable is also set to a system-specific error code on failure.

Example

/* remove example: remove myfile.txt */ int main () < if( remove( "myfile.txt" ) != 0 ) perror( "Error deleting file" ); else puts( "File successfully deleted" ); return 0; >

If the file myfile.txt exists before the execution and the program has write access to it, the file would be deleted and this message would be written to stdout:

File successfully deleted 

Otherwise, a message similar to this would be written to stderr:

Error deleting file: No such file or directory 

Источник

std::filesystem:: remove, std::filesystem:: remove_all

1) The file or empty directory identified by the path p is deleted as if by the POSIX remove . Symlinks are not followed (symlink is removed, not its target).

2) Deletes the contents of p (if it is a directory) and the contents of all its subdirectories, recursively, then deletes p itself as if by repeatedly applying the POSIX remove . Symlinks are not followed (symlink is removed, not its target).

Contents

[edit] Parameters

[edit] Return value

1) true if the file was deleted, false if it did not exist. The overload that takes error_code& argument returns false on errors.

2) Returns the number of files and directories that were deleted (which may be zero if p did not exist to begin with). The overload that takes error_code& argument returns static_cast < std:: uintmax_t > ( — 1 ) on error.

[edit] Exceptions

The overload that does not take a std:: error_code & parameter throws filesystem::filesystem_error on underlying OS API errors, constructed with p as the first path argument and the OS error code as the error code argument. The overload taking a std:: error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails.

[edit] Notes

On POSIX systems, this function typically calls unlink and rmdir as needed, on Windows RemoveDirectoryW and DeleteFileW .

[edit] Example

#include #include #include namespace fs = std::filesystem; int main() { fs::path tmp = std::filesystem::temp_directory_path(); std::filesystem::create_directories(tmp / "abcdef/example"); std::uintmax_t n = fs::remove_all(tmp / "abcdef"); std::cout  "Deleted "  n  " files or directories\n"; }
Deleted 2 files or directories

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3014 C++17 error_code overload of remove_all marked noexcept but can allocate memory noexcept removed

Источник

std:: remove

Deletes the file identified by character string pointed to by fname .

If the file is currently open by the current or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name, although the file system space is not reclaimed even if this was the last hardlink to the file until the last running process closes the file, Windows does not allow the file to be deleted)

Contents

[edit] Parameters

[edit] Return value

​ 0 ​ upon success or non-zero value on error.

[edit] Notes

POSIX specifies many additional details for the behavior of this function.

The standard library also defines a function template std::remove taking a pair of iterators and a value, this overload is one of the standard algorithms.

[edit] Example

#include #include #include int main() { bool ok = static_castbool>(std::ofstream("file1.txt").put('a')); // create file if(!ok) { std::perror("Error creating file1.txt"); return 1; } std::cout  <std::ifstream("file1.txt").rdbuf()  <'\n'; // print file std::remove("file1.txt"); // delete file bool failed = !std::ifstream("file1.txt"); if(failed) { std::perror("Error opening deleted file"); return 1; } }
a Error opening deleted file: No such file or directory

Источник

Оцените статью