Asked by:
Add lines in file without deleting the existing one

Question
-
User-106287432 posted
Hello!
I have text file which change information only on header.
My code:List<byte> byteList = new List<byte>(); using (var input = File.OpenText(fileName)) { string line, newLine = ""; int i = 0; while (null != (line = input.ReadLine())) { if (line.Contains("End")) { byteList.AddRange(Encoding.UTF8.GetBytes(line + Environment.NewLine)); break; } else byteList.AddRange(Encoding.UTF8.GetBytes(line + Environment.NewLine)); i++; } } using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate)) { fileStream.Seek(0, SeekOrigin.Begin); byte[] arr = byteList.ToArray(); fileStream.Write(arr, 0, arr.Length); }
It is working fine. But now i want to add 3 more lines when i find line:"Marker Name". I tried it like that:
if ( line.Contains("MARKER NAME")) { newLine = Environment.NewLine + Environment.NewLine + Environment.NewLine; line = newLine + line; }
These 3 lines are added but 3 lines after "End " are missing because of break if (line.Contains("End")). I need this because i don`t want to read the whole file.
it is working fine if i don`t have break after "End" but is there any way to add 3 lines without reading the whole file? I only need the information before that lines.
Thank youWednesday, April 7, 2021 12:07 PM
All replies
-
User1535942433 posted
Hi HoneyMG,
According to your description,I don't understand your requirement clearly.I have doubts.
1.What's the relationship of "MARKER NAME" and "End" in lines? Are they in the same line or randomly in whole files?
2.How do you write the logic of codes of Contains("End") and Contains("MARKER NAME")?
Could you tell more details to us?It will help us to solve your problems.
Best regards,
Yijing Sun
Thursday, April 8, 2021 3:13 AM -
User-106287432 posted
Hello, Yijing Sun!
The file is actually RinexFile and i only change the header. Marker Name is line and i have to add these 3 new linews above it
The whole logic is that file is read line by line when reach "Marker Name" it add 3 new lines before it. Then after it reached the "End" it break brecause i don`t change the information after it. Every line is stored in list "bytes" and with filestream they are add to file.
It is working fine if i read the whole file line by line. By i don`t want that because the file can be very large and that process is going to be slow.
Let me explaing what i want(not the right file, but you can get what i mean):
The file is looking something like this:line1 line2 line3 line4 line5 MarkerName line6 END line7 line8 line9 line10 .....
After I add these lines i want to look like this:
line1 line2 line3 line4 line5 MarkerName line6 END line7 line8 line9 .....
But i get:
line1 line2 line3 line4 line5 MarkerName line6 END line10 .....
Thursday, April 8, 2021 4:28 AM -
User303363814 posted
Where did you put the extra code? Show the whole method.
Thursday, April 8, 2021 4:47 AM -
User-106287432 posted
The whole method:
public static string ModifyHeader(string fileName) { if (!File.Exists(fileName)) { Logger.GetLogger.Error("File does not exist {0}.", fileName); return String.Empty; } try { List<byte> byteList = new List<byte>(); // Open the text file using a stream reader. using (var input = File.OpenText(fileName)) { string line, newLine = ""; int i = 0; while (null != (line = input.ReadLine())) { if ( line.Contains("MARKER NAME")) { newLine = Environment.NewLine+ Environment.NewLine+ Environment.NewLine; line = newLine + line; } if (line.Contains("End") { byteList.AddRange(Encoding.UTF8.GetBytes(line + Environment.NewLine)); break; } else byteList.AddRange(Encoding.UTF8.GetBytes(line + Environment.NewLine)); i++; } } using (FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate)) { fileStream.Seek(0, SeekOrigin.Begin); byte[] arr = byteList.ToArray(); fileStream.Write(arr, 0, arr.Length); } } catch (Exception e) { Logger.GetLogger.Fatal("One or more exceptions occurred:"); Logger.GetLogger.Fatal(" {0}: {1}", e.GetType().Name, e.Message); } return fileName; }
Thursday, April 8, 2021 6:16 AM -
User303363814 posted
Set a breakpoint and check what is happening.
Is a 'line' what you think it is? For each iteration of the loop, investigate what 'line' is and what byteList is at the start and end of each pass.
You do realise that "End".Contains("END") == false
Not really sure what you are trying to do as your sample of the desired output has lines after "END" but the comments you made imply that you want to stop at "END".
My 2c
var result = new List<string>(); foreach(var line in File.ReadLines(filename).TakeWhile(f => !f.Contains("END"))) { if (line.Contains("MARKER")) { result.Add(string.Empty); result.Add(string.Empty); result.Add(string.Empty); } result.Add(line) } result.Add("END");
Thursday, April 8, 2021 7:11 AM -
User-106287432 posted
These 3 new lines are created. My problem is when i have to add it to file again with FileStream.
What i get is that i have created lines but 3 lines after "End" are missing(deleted).Thursday, April 8, 2021 7:17 AM -
User303363814 posted
What does the debugger show?
On which precise iteration of the loop does the first variable have a value that you do not expect? Which variable? What value are you expecting? What value do you get?
Run again and stop before the problem iteration. Now step a single statement at a time. On which exact, precise statement does a variable get set to a value that you do not want?
Detail, detail, detail.
Thursday, April 8, 2021 7:57 AM -
User753101303 posted
Hi,
Seems fine and I even tried with your sample file to make 100% sure. Either you are not showing the actual code (for example you have MarkerName in the sample file and MARKER NAME in your code) or maybe you lost those lines in a previous attempt ?
It seems that at some point you have overwritten old content with those new lines rather than really inserting them. Try with your sample file.
Thursday, April 8, 2021 8:34 AM