how can i delete a line in the textfile using c++?

delete:
{
int x,y=0;
char str[100];
char tempid[100];
clrscr();
fstream my_file;
my_file.open("data.txt",ios::in|ios::out);
if(my_file.fail())
{
cout<<"\nFile not opened";
}
else
{
cout<<"Student Number: ";
gets(tempid);
while(!my_file.eof())
{
my_file.getline(str,100);
x=strcmp(tempid,str);
if (x==0){
goto a;
}
else{
trapsearch();
goto again;
}
}
a:
while(y<4){
/* IDONT KNOW HOW TO DELETE a LINE FROM THE TEXTFILE.*/
y++;
}
getch();
my_file.close();
goto again;
}}
/* IDONT KNOW HOW TO DELETE a LINE FROM THE TEXTFILE.*/


You want to delete a line from the textfile that you are reading ? This is very tricky. When you read the file line by line, the file pointer is advancing forward to be ready to read the next line. If you want to delete the previous line that is read, you need to rewind your file pointer back.

There are fseek, ftell, rewind, fgetpos, fsetpos etc calls but still it is very tricky. Usually for such file-centric operations, I turn to shell scripts or Perl. I recommend Perl for it's ease in file io manipulation :)
This is a common question.

The simplest answer in C++ is to simply load the file into a std::deque <std::string>, delete the Nth item, and then overwrite the file.

The other standard option is to copy the file, line by line, to a new file, making the desired changes as you go. Once done, delete the original file and rename the new file as the original.

Hope this helps.
I think u should pass empty string to that line.expart will finish ;-)
how can i pass empty string to that line?
The other standard option is to copy the file, line by line, to a new file, making the desired changes as you go. Once done, delete the original file and rename the new file as the original.


-- i've tried it but it deletes all the contents of the old file. i can.t copy it line by line. here's the code..
can you help me please?

delete:
{
int x,y=1;
char str[100];
char tempid[100];
clrscr();
fstream my_file;
my_file.open("data.txt",ios::in);
FILE * myfile;
FILE *ft;
ft=fopen("temp.txt","wb");
myfile=fopen("data.txt","rb+");
cout<<"Student Number: ";
gets(tempid);
while(!my_file.eof())
{
my_file.getline(str,100);
x=strcmp(tempid,str);
if (x==0){
goto a;
}
else{
trapsearch();
goto again;
}
}
a:
rewind(myfile);
fclose(myfile);
fclose(ft);
remove("data.txt");
rename("temp.txt","data.txt");
myfile=fopen("data.txt","rb+");
fclose(myfile);
goto again;
}
I can't follow your code, where is the label again? (goto is evil, you can easily avoid it with loops and functions calls)
I don't see where do you write to the file either.
Is trapsearch() relevant? What does it do?
You are trying to mix C and C++ file I/O. That won't work. Read up on the primer for working with files
http://www.cplusplus.com/doc/tutorial/files/

And yes, don't use goto for this. Use loops.

Finally, don't test against EOF. Your loop should test against failure to read a line.

Hope this helps.
trapsearch is to check if the entered student number don't exist.

i've already solved the problem.
thankyou :)
Topic archived. No new replies allowed.