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.
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
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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:
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");
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).
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?
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.
Member
1 Points
19 Posts
Add lines in file without deleting the existing one
Apr 07, 2021 12:07 PM|HoneyMG|LINK
Hello!
I have text file which change information only on header.
My code:
It is working fine. But now i want to add 3 more lines when i find line:"Marker Name". I tried it like that:
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 you
Contributor
4060 Points
1578 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 03:13 AM|yij sun|LINK
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
Member
1 Points
19 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 04:28 AM|HoneyMG|LINK
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:
After I add these lines i want to look like this:
But i get:
Participant
1660 Points
952 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 04:47 AM|PaulTheSmith|LINK
Where did you put the extra code? Show the whole method.
Member
1 Points
19 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 06:16 AM|HoneyMG|LINK
The whole method:
Participant
1660 Points
952 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 07:11 AM|PaulTheSmith|LINK
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
Member
1 Points
19 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 07:17 AM|HoneyMG|LINK
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).
Participant
1660 Points
952 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 07:57 AM|PaulTheSmith|LINK
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.
All-Star
48730 Points
18190 Posts
Re: Add lines in file without deleting the existing one
Apr 08, 2021 08:34 AM|PatriceSc|LINK
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.