Link to home
Start Free TrialLog in
Avatar of CaptainI
CaptainI

asked on

Deleting specific line from file

Hail and well met,

I've been busy with files lately and since I am fairly new to the concept of reading and writing files I was wondering if anyone could tell me how to delete a specific line from a file.

for example a txt file is filled with


aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
ccccccccccccccccccc
dddddddddddddd

Now I would like to delete line cccccccccccccc so that I end up with

aaaaaaaaaaaaaa
bbbbbbbbbbbbbb
dddddddddddddd

so far I managed to use seek functions to determine the end and the beginning of the line and I can easily overwrite it with  blank spaces. But I don't want a blank line in my file and I furthermore don't want to read the entire file into a buffer and then write it back once I am done deleting (since my file is... big)

This is what I have so far (minus some redundant code):

while (fileLoader.ReadString(strLine)){
if (strLine == CurrentMess){ // currentmess is known
      FilePos = fileLoader.GetPosition(); // retrieve position
      FilePos -= FileOffset+2; // file offset is the length of currentmess
      fileLoader.Seek(FilePos, CFile::begin); // go to beginning of deletable line
      for (int index = 0; index != FileOffset;index++)
                      fileLoader.Write(" ",1); // overwrite with spaces
      fileLoader.Close();
}


never mind the syntax I deleted some junk from it probably causing it to not compile anymore.

Either way rather than just overwriting the line with blanks I'd rather remove the entire line (including \n)

Any ideas?

Thanks in advance,

Cap
Avatar of r-k
r-k

There are probably some tricky ways of doing what you want, but I don't
think they would be better than just creating a new file, esp. if you want
your program to be system independent.
Avatar of CaptainI

ASKER

Yeah I have been poking around at it for a few hours now and I guess it is easier to make a new file. If anyone could give me a hint on how it would be done without creating a new file I'd appreciate it, now I just plain want to know hehe.

If someone manages to provide me with a decent indication I'll drop the 50 points on that person otherwise I'll just attempt to claim them back.

ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial