-
deleting files?
Hi people,
just started programming bout a week ago and need a little help with some file i/o
what i have done so far is use fstream to open save and then close a file in which the user types the filename and its contents but i have no idea how to delete the file any help??
cheers in advance
:confused:
-
You can use remove(char* filename) to do this task.
-
thanx for the help but i cannot remove(char* filename) to work :(
here is some of my code
Code:
ofstream file;
file.open(fileName);
file<<firstName<<"\n"
<<lastName<<"\n"
<<phoneNumber<<"\n"
<<mobilePhone<<"\n"
<<emailAddress<<"\n";
file.close();
return menu();
}
int deleteContact()
{
file.remove("blah.txt");
return menu();
}
i know i am useing it totaly wrong, any help on how to implament it?
thanx
-
remove() is not a ofstream member function, if you include <cstdio> it will be a global function so you can call it like this -
int deleteContact()
{
remove("blah.txt");
return menu();
}
assuming that blah.txt is in your current directory. Otherwise you'll have to include the full path for the file.
-
cheers Zen that work perfectly :)
yet another question how can i make cin.getline to terminate when the user presses enter with out haveing to make them type the ternial_char first.
cheers
-
The default behavior of getline is to terminate when a newline is entered. I'm not sure what you mean by ternial_char, but is quite easy with i/o to leave characters in the buffer that affect how getline works. Post the code.
-
Ooops my bad. I neglected to say that remove() is a regular c function. I'll be more specific next time.