0
case 4:
 {
  cout<<"Enter the word you want to delete: ";
  gets(b);
  f1.open("anotherf.txt",ios::in);
  f2.open("tempf.txt",ios::out);
  while(!f1.eof())
  {
   f1>>a;
   i++;
   if(strcmp(a,b)==0)
   {
    j=i;
   }
  }
 i=0;

 while(!f1.eof())
 {
   if(i!=j)
   {
    f1>>a;
    f2<<a;
   }
  i++;
 }
 f1.close();
 f2.close();
 f2.open("tempf.txt",ios::in);
 f2.seekg(0);
 f1.open("anotherf.txt",ios::out);
 while(!f2.eof())
 {
  f2>>a;
  f1<<a;
 }
 goto menu;
 }

I am trying to delete a certain element from a file. However when i run it it only displays blank. And from testing it, i THINK it doesn't even reach the part of the code where the information is passed from the second file back to the first.Im new to file handling, please help! Tha

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by NathanOliver
0

well one thing i can see is that you never reset the file stream to the begining when you go from your first while loop to your second while loop. You really need to indent your code better. As is it is very hard to follow. Here is an example of some better indentation.

int main()
{
    int foo = 1;
    int bar = 2;
    if(foo == bar)
    {
        // do something
    }
    else
    {
        if (bar - 1 == foo)
        {
            // do some more
        }
        else
        {
            // do something else
        }
    }
    cin.get();
    return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.