0

I'm trying to read a file line by line, search for a string in the line, need to replace it with a different string without overwrite & without using another file to save the information.
So far I got:

public void InsertOrUpdate(string s) {
            string line = "";
            StreamReader file = new StreamReader(this.filename);
            while ((line = file.ReadLine()) != null) {
                if (line.Contains(s)) {
                    // This found line I want to replace.
                }
            }
        }
barakadax
  • 47
  • 9
  • In this answer the guy who answered gave 2 options, the first one is loading the whole file into the memory but I don't know if it's gonna be good because I don't know the file size, the other option he wrote is using a different file to help so I can't use that. – barakadax Mar 09 '21 at 18:18
  • 5
    There are 5 answers to that question,did you read all of them? Also, did you read the accepted answer warning? `You can't rewrite a line without rewriting the entire file (unless the lines happen to be the same length)` and you they say same length, they are meaning in bytes, not characters. – Magnetron Mar 09 '21 at 18:23
  • None of them did what I was looking for, thanks for trying. – barakadax Mar 09 '21 at 19:56
  • 2
    It's pretty rare to have a true fixed width record file where you can access any "record"/line and directly overwrite just its contents. You're not finding a good answer because the answer is not what you want to hear: **You have to overwrite the whole file for just one line, or use a separate file.** – Idle_Mind Mar 09 '21 at 21:16
  • You are totally right my good sir, I was asked to try & write this AND, I couldn't figure how, even tried 'r+' & using seek to rewrite but because the fields are not fixed by size it's an issue. – barakadax Mar 09 '21 at 21:26
  • This sounds like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) - perhaps you can find out why the requirements are as they are, and then you can fix it. For example, you could change a line in the file and then move the entire rest of the file up or down as necessary without creating a new file. – NetMage Mar 09 '21 at 21:55
  • Why is the method named `InsertOrUpdate`? – NetMage Mar 09 '21 at 22:02
  • I wanted to do that but I have another requirement for not overwriting the file, so I can't, was asked to finished an object with such weird requirements... I was given this method & need to write it without overwriting the file & without using another file, only those 2. – barakadax Mar 09 '21 at 22:30
  • 1
    Store the byte position of the beginning of the line. New line needs to be <= length of existing line. For new shorter lines, pad with blanks. Then the rest of the file doesn't move. Store all the positions/lines in a List<>. Close the file, then open for writing. [Seek](https://docs.microsoft.com/en-us/dotnet/api/system.io.stream.seek?view=net-5.0) to the positions and write. [BaseStream](https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader.basestream?view=net-5.0) and [Position](https://docs.microsoft.com/en-us/dotnet/api/system.io.stream.position?view=net-5.0) may be helpful. – Idle_Mind Mar 09 '21 at 22:58

0 Answers0