Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I'm trying to remove a line from a text file, just something simple. I have a text file that has user information inside:
UID: X Firstname: XXXXX Lastname: XXXXX Date Added: XXXXX

So, what I'm trying to do is let the a "admin" input the UID(user id), and delete the line with that only. I don't know if that's possible because it's not working for me.
My code below is not correct I know, but I can't find out how to do it.

What I have tried:

C#
Console.Write ("Enter the userID: ");
userID = Console.ReadLine ();

int lineNumber = 0;
string line;

var sr = new StreamReader (combinePath);
while((line = sr.ReadLine()) != null){
    if (line.Contains (userID)){
        line.Remove (lineNumber , lineNumber++);
    }
    lineNumber++;
}
Posted
Updated 27-Feb-20 0:29am
v2

You know it works this way
  • Open the original file for reading and read it line by line.
  • Copy only the wanted lines to another file (the 'output' file, that is open for writing)
  • At the end of the process, delete the original file and rename the output one.
 
Share this answer
 
Comments
Maciej Los 27-Feb-20 6:23am    
5ed!
CPallini 27-Feb-20 6:44am    
Thank you, Maciej!
Test files don't contain "lines".

Yes, yes, I know - you can read and write lines, so obviously they do contain them. But in practice, they don't - they contain a stream of characters some of which are interpreted as a "newline" by software reading the file. When you "read a line", the software reads each character until it gets to the next newline character (in C#, you can see this as '\n') and it returns all the characters it read as a single string - the "line". When you write a line, your text is written to the string and a newline character appended to that.

That's important to know, because it means you can't just "delete a line" or "insert a line" into a text file. To do that you have to create a new file.
To Delete a line:
1) Copy all the text up to the start of the line from the old file to the new file.
2) Find the end of the line to delete without copying it.
3) Copy all the text up to the end of the file from the old file to the new file.
4) Close both files.
5) Delete the original file, and rename the new file to become the old one.


To Insert a line:
1) Copy all the text up to the start of the line before which you want to insert the new line from the old file to the new file.
2) Write your new text to the new file, with a newline character at the end.
3) Copy all the text up to the end of the file from the old file to the new file.
4) Close both files.
5) Delete the original file, and rename the new file to become the old one.
 
Share this answer
 
v2
Comments
Maciej Los 27-Feb-20 6:23am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900