please help me, How do I delete line in a txt file and how to make an edit funtion?

Im having trouble, the program will create two file, one is a txt file (to display all the movie name) and the other is the file to store the information of the movie. My delete function is not able to delete the movie name in the txt file but its able to delete the movie information file. One more thing, my edit function can't over ride my data but it just adds more line in the movie file if I choose the same name of the movie and if I try to edit the movie name, it creates a new file for it and it doesn't delete the original file and it deletes every name in the txt file. Please help me as soon as possible.

Here is my code :




#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <ctime>
using namespace std;

double rating;
int reviewcount, numtopreview=0;
char name[50], summary[30], summary2[300], topreview[300], director[50], studio[20];

void ask();
void PBnJ();
void decor();
void time();
void menu();
void add();
int edit();
bool fileExists(const char *fileName);
void del();
int view();
int sea();

int main()
{
char help;
menu();
return 0;
}

void ask()
{
char help;
cout << endl;
cout << "Do you want to continue? [Y/N]: "; cin >> help;
if((help == 'Y') || help == 'y')
{
system("cls");
main();
}
else if ((help == 'N') || (help == 'n'))
{
cout << "Thank you for using PBnJ MOVIE REVIEW SYSTEM!" << endl;
cout << "Please come again." << endl;
cout << endl;
}
else
{
cout << "Invalid input. Please try again.";
ask();
}
}

void pbnj()
{
cout << "*******************************************************************************" << endl;
cout << " " << endl;
cout << " *************** ************* *************** " << endl;
cout << " *************** ************** *************** " << endl;
cout << " *** *** *** *** *** " << endl;
cout << " *** *** *** *** *** " << endl;
cout << " *** *** *** *** *** " << endl;
cout << " *** *** *** *** *** " << endl;
cout << " *************** *************** *** " << endl;
cout << " *************** *************** ************** *** " << endl;
cout << " *** *** *** *************** *** " << endl;
cout << " *** *** *** *** *** *** " << endl;
cout << " *** *** *** *** *** *** " << endl;
cout << " *** *** *** *** *** *** *** " << endl;
cout << " *** ************** *** *** ********* " << endl;
cout << " *** ************* *** *** ******* " << endl;
cout << " " << endl;
cout << "*******************************************************************************" << endl;
}

void decor()
{
cout << "*******************************************************************************" << endl;
}

void time()
{
time_t now = time(0);
char* dt = ctime(&now);
cout << dt;
}

void menu()
{
int veg;
pbnj();
cout << "\t\t WELCOME TO PBnJ MOVIE REVIEW SYSTEM" << endl;
cout << " What would you like to do?" << endl;
cout << " |1| Add movie review" << endl;
cout << " |2| Edit movie review" << endl;
cout << " |3| Delete movie review" << endl;
cout << " |4| View all movie reviews" << endl;
cout << " |5| Search movie review" << endl;
cout << " |6| Exit" << endl;
cout << endl;
cout << "Please select an option: "; cin >> veg;
cin.clear();
cin.sync();

if ((!veg) || (veg < 0) || (veg > 6))
{
cout << "Invalid input. Please try again." << endl;
system("pause");
system("cls");
menu();
}

switch(veg)
{
case 1:
system("pause");
system("cls");
add();
break;
case 2:
system("pause");
system("cls");
edit();
break;
case 3:
system("pause");
system("cls");
del();
break;
case 4:
system("pause");
system("cls");
view();
break;
case 5:
system("pause");
system("cls");
sea();
break;
case 6:
break;
}
ask();
}

void add()
{
time(); decor();
cout << "Name of movie : "; cin.getline(name, 50);
fstream file(name, ios::in|ios::out|ios::app);
fstream zfile("movies.txt", ios::in|ios::out|ios::app);
zfile << name << endl;
zfile.close();

file << "Movie name : " << name << endl;
do
{
cout << "Average rating [?/10] : "; cin >> rating;
if((!rating) || (rating > 10) || (rating < 0))
{
cout << "Invalid input. Please try again." << endl;
}
cin.clear();
cin.sync();
}
while ((!rating) || (rating > 10) || (rating < 0));

file << "Average rating : " << rating << endl;

do
{
cout << "Review count : "; cin >> reviewcount;
if((!reviewcount) || (reviewcount < 0))
{
cout << "Invalid input. Please try again." << endl;
}
cin.clear();
cin.sync();
}
while ((!reviewcount) || (reviewcount < 0));

file << "Review count : " << reviewcount << endl;

cout << "Summary : "; cin.getline(summary, 300);
file << "Summary : " << summary << endl;

cout << "Directed by : "; cin.getline(director, 50);
file << "Directed by : " << director << endl;

cout << "Studio : "; cin.getline(studio, 20);
file << "Studio : " << studio << endl;

do
{
cout << "Number of top reviews : "; cin >> numtopreview;
if((!numtopreview) || (numtopreview < 0))
{
cout << "Invalid input. Please try again." << endl;
}
}
while ((!numtopreview) || (numtopreview < 0));

cin.ignore(1000, '\n');
for (int i=0; i<numtopreview; i++)
{
cout << "Top review " << i+1 << " : "; gets(topreview);
file << "Top review " << i+1 << " : " << topreview << endl;
}
file.close();
}

int edit()
{
char mname[50];
time(); decor();
ifstream vfile("movies.txt");

if(!vfile)
{
cout << "Cannot open file.";
return 1;
}

char str2[255];

while(vfile)
{
vfile.getline(str2, 255);
if(vfile) cout << str2 << endl;
}
vfile.close();

cout << endl;

fstream bfile("movies.txt", ios::in|ios::out);
fstream nfile("temp.txt", ios::in|ios::out);
do
{
cout << "Movie name : "; gets(mname);
}
while(!mname);

char joke[50];

if(!bfile)
{
cout << "Cannot open file.";
return 1;
}

char str3[255];

while(bfile)
{
bfile.getline(str3, 255);
if(bfile != mname) nfile << str3 << endl;
}
bfile.close(); nfile.close();

remove ("movies.txt"); rename("temp.txt", "movies.txt");
add();
}

bool fileExists(const char *fileName)
{
ifstream infile(fileName);
return infile.good();
}

void del()
{
char nname[50];
time(); decor();
cout << "Movie name : "; gets(nname);
remove(nname);
cout << endl;
cout << nname << " has been deleted.";
}

int view()
{
char bname[50];
time(); decor();
ifstream xfile("movies.txt");

if(!xfile)
{
cout << "Cannot open file.";
return 1;
}

char str1[255]; int num=0;

while(xfile)
{
num = num + 1;
cout << "[" << num << "] ";
xfile.getline(str1, 255);
if(xfile) cout << str1 << endl;
}
xfile.close();
}

int sea()
{
char sear[50], vname[50];
time(); decor();
cout << "Search a movie : "; gets(sear); decor();
ifstream cfile(sear);

if(!cfile)
{
cout << "Cannot open file.";
return 1;
}

char str[255];

while(cfile)
{
cfile.getline(str, 255);
if(cfile) cout << str << endl;
}

cfile.close();
}
Topic archived. No new replies allowed.