Problem with C # text creation

This is what happens. I have a huge text file, which should be 1 line per record. The problem is that sometimes a line breaks into a new line.

I am editing this whole file and wherever the file starts ("\" A), I need to add the current line to the previous line (replacing \ n with ""). All I came up with is adding a line to a new line. Any help is available ...

THE CODE:

public void step1a() { string begins = ("\"A"); string betaFilePath = @"C:\ext.txt"; string[] lines = File.ReadAllLines(betaFilePath); foreach (string line in lines) { if (line.StartsWith(begins)) { File.AppendAllText(@"C:\xt2.txt",line); File.AppendAllText(@"C:\xt2.txt", "\n"); } else { string line2 = line.Replace(Environment.NewLine, " "); File.AppendAllText(@"C:\xt2.txt",line2); } } } 

Example: Orig:
"\" "Hero | apple | orange" for the pleasure of this | "\" "Hero | apple | mango | lots of fun always

"No \" A "Her | apple | fruit | no
pain is the way "\" A "Hero | love | stackoverflowpeople | more fun

RESULT OF:
"\" "Hero | apple | orange" for the pleasure of this | "\" "Hero | apple | mango | lots of fun always

"\" "Her | apple | fruit | no pain - the way |" \ "A" Hero | love | stackoverflowpeople | More fun

My problem is not finding if (line.StartsWith (starts)) its else statement, it adds line2 to a new line

+4
source share
4 answers

This does what you want:

  CopyFileRemovingStrayNewlines(@"C:\ext.txt", @"C:\xt2.txt", @"""\""A"); 

With this method:

 public static void CopyFileRemovingStrayNewlines(string sourcePath, string destinationPath, string linePrefix) { string[] lines = File.ReadAllLines(sourcePath); bool firstLine = true; foreach (string line in lines) { if (line.StartsWith(linePrefix)) { if (!firstLine) File.AppendAllText(destinationPath, Environment.NewLine); else firstLine = false; File.AppendAllText(destinationPath, line); } else { File.AppendAllText(destinationPath, " "); File.AppendAllText(destinationPath, line); } } } 

He has a problem adding an existing file. I suggest using a StreamWriter , not AppendAllText . Like this:

 public static void CopyFileRemovingStrayNewlines(string sourcePath, string destinationPath, string linePrefix) { string[] lines = File.ReadAllLines(sourcePath); bool firstLine = true; using (StreamWriter writer = new StreamWriter(destinationPath, false)) { foreach (string line in lines) { if (line.StartsWith(linePrefix)) { if (!firstLine) writer.WriteLine(); else firstLine = false; writer.Write(line); } else { writer.Write(" "); writer.Write(line); } } } } 
0
source

it looks like your string is poorly formed ...

try instead "\"\\\"A\""

 public void step1a() { string begins = ("\"\\\"A\""); string betaFilePath = @"C:\ext.txt"; string[] lines = File.ReadAllLines(betaFilePath); foreach (string line in lines) { if (line.StartsWith(begins)) { File.AppendAllText(@"C:\xt2.txt",line); File.AppendAllText(@"C:\xt2.txt", "\n"); } else { string line2 = line.Replace(Environment.NewLine, " "); File.AppendAllText(@"C:\xt2.txt",line2); } } } 
+2
source

Your problem is that \ is the C # exit code.
Your string is parsed as "A because \" is the exit code for one. "

You must make the string begins string @ -string, which does not use escape codes.
Then you will need to avoid " by doubling it. For example:

 const string begins = @"\""A"; 

Note that the best way to do this is to use StreamWriter , for example:

 using(StreamWriter writer = File.Create(@"C:\xt2.txt")) { foreach (string line in lines) { if (line.StartsWith(begins)) writer.WriteLine(); //Close the previous line writer.Write(line); } } 
0
source

Based on the @SLaks example, here is the code that should do the trick:

  public static void step1a() { string betaFilePath = @"C:\ext.txt"; string[] lines = File.ReadAllLines(betaFilePath); using (StreamWriter writer = new StreamWriter(File.Create(@"C:\xt2.txt"))) { string buffer = null; foreach (string line in lines) { if (!line.StartsWith(begins)) { writer.WriteLine(buffer + line); buffer = null; } else { if (buffer != null) writer.WriteLine(buffer); buffer = line; } } if(buffer != null) Console.Out.WriteLine(buffer); } } 
0
source

Source: https://habr.com/ru/post/1305014/


All Articles