0

I am currently working on a project where i must remove a specfic line of text from a text file.

This is the code i have already:

static void Main( string[] args )
    {
        string line = null;
        string line_to_delete = "--";
        string desktopLocation = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string text = Path.Combine( desktopLocation, "tim3.txt" );
        string file = Path.Combine( desktopLocation, "tim4.txt" );

        using (StreamReader reader = new StreamReader( text))
        {
            using (StreamWriter writer = new StreamWriter(file ))
            {
                while((line = reader.ReadLine()) != null)
                {
                    if (string.Compare( line, line_to_delete ) == 0)
                        continue;
                    writer.WriteLine( line );
                }
            }

This writes only the text to a new txt file but doesnt delete anything.

Thanks. The main problem is that i just need to delete a specific line of text in the txt file.

Artic-M00n
  • 533
  • 2
  • 8
  • 15
  • Have you run into debugger? This should be simple to solve – CharlesB May 16 '12 at 09:25
  • Well i havent got any errors but when i run it, it only copies all the text frm te first txt file to the second. it doesnt delete the text or character i specify. – Artic-M00n May 16 '12 at 09:27
  • Take in to account the line endings. Show the input data. Debug can help you to anderstand what different in strings and why comparer does't find equals strings – gabba May 16 '12 at 09:29
  • this may help: http://stackoverflow.com/questions/668907/how-to-delete-a-line-from-a-text-file-in-c – Mohammad Sepahvand May 16 '12 at 09:31

3 Answers3

0

I suspect leading/training spaces, which are causing String.comapre to return unexpected result

Instead of

 if (string.Compare( line, line_to_delete ) == 0)

try

if(String.IsNullOrEmpty(line) || String.Equals(line.Trim(), line_to_delete))

As above is not working can you try following (LINQ)

EDIT - to replace only text

File.WriteAllText(file, File.ReadAllText(text).Replace(line_to_delete,"")) 

If above works, it will replace all using block in your code

If above does not work, i suspect it is data issue ("__" not present in text file)

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • String.IsNullOrEmpty(line) || will skip empty lines, i suposed it's is not correct – gabba May 16 '12 at 09:31
  • I just tried this code but it gives me the same result. it just writes the same text over again. – Artic-M00n May 16 '12 at 09:32
  • 1
    It is difficult to understand what's going on. I would try to put a breakpoint on the if line conditioned by line.Contains(line_to_delete); and then quickwatch the two strings – Francesco Baruchelli May 16 '12 at 09:38
  • Thanks, the code is working now but i have to specify the entire line in order to delete. I have two -- before my text. how can i specify that only the lines with -- must be removed? – Artic-M00n May 16 '12 at 09:47
  • ok, you want only text to be removed, remaining portion should remain intact, right. Hold on – Tilak May 16 '12 at 09:52
  • You can use line.Contains(line_to_delete) or line.StartsWith(line_to_delete) to check for a match, as Im guessing you want to delete comments? Note that contains also deletes lines like 'blabla bla -- bla blabla' and startswith will match lines like '-- this is a comment'. – M.Schenkel May 16 '12 at 09:53
  • @Artic, updated for replacing only __, not skipping remaining line – Tilak May 16 '12 at 09:56
  • ok i just ealised that all my programme is doing now is deleting only the characters in the comment and it wont remove it with the text. – Artic-M00n May 16 '12 at 11:37
0

As far as I know, you can't read AND write to a file at the same time (file locking). What you could do, is write the appropriate data to tim4.txt (like in your example) and when youre done, delete tim3.txt and rename tim4.txt to tim3.txt.

EDIT: Mis interpreted your question. I thought you could't write your changes to the same file, but you're text is not even filtered. You should try Tilan's suggestion

M.Schenkel
  • 477
  • 1
  • 3
  • 9
  • well i hav seen alot of people suggest that way. i have no idea on how i will go about doing this. – Artic-M00n May 16 '12 at 09:33
  • It is actually pretty easy. After your using statements (when all IO reads/writes are done) insert a simple try / catch block and call File.Delete(file_to_delete). Then you need to rename your new file (tim4) to the name of your old (tim3). Since C# does not provide a rename function, you can call File.Move(old_file,new_file). Does this clear things up? – M.Schenkel May 16 '12 at 09:39
  • Found someone who posted the code exactly as I meant it: http://stackoverflow.com/a/668917/1222748 – M.Schenkel May 16 '12 at 09:41
0

From your comments I understand that you are trying to delete the lines starting with --. If this is the case you just have to change the if condition from:

            if (string.Compare( line, line_to_delete ) == 0)
                continue;

to:

            if (line.Trim().IndexOf(line_to_delete) == 0)
                continue;
Francesco Baruchelli
  • 7,320
  • 2
  • 32
  • 40