#include <stdio.h>
//required header
The remove(
) and
rename( )
functions in the stdio.h
library
can be used to manipulate files.
The remove(
) function deletes a file from
memory. For example, remove("myfile.dat); deletes myfile.dat
from memory. The remove( ) function requires a C-style string as an
argument. If an apstring type variable is used as an argument, you must
engage the .c_str( ) to convert it to a standard C++ string variable.
//Remove a file
#include<iostream.h>
#include<fstream.h>
#include<stdio.h> //for remove( ) and rename( )
#include "apstring.cpp"
int main(void)
{
apstring fileToDelete;
cout<<"Enter name of file to delete: ";
getline(cin,fileToDelete);
if (remove(fileToDelete.c_str( )) !=0)
cout<<"Remove operation failed"<<endl;
else
cout<<fileToDelete<<" has been removed."<<endl;
return 0;
}
The rename( ) function takes the
name of a file as its argument, and the new name of the file as a second
argument. For example, rename("myfile.dat", "newfile.dat");
renames the file myfile.dat as newfile.dat. The rename( ) function also
requires C-style string arguments.
|