开发者

C# text creation issue

开发者 https://www.devze.com 2022-12-25 00:44 出处:网络
This is whats going on.I have a huge text file that is suppose to be 1 line per entry.The issue is sometimes the line is broken with a new line.开发者_StackOverflow

This is whats going on. I have a huge text file that is suppose to be 1 line per entry. The issue is sometimes the line is broken with a new line.

开发者_StackOverflow

I edit this entire file and wherever the file doesn't begin with ("\"A) i need to append the current line to the previous line ( replacing \n with " "). Everything I come up with keeps appending the line to a new line. Any help is appricated...

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:

"\"A"Hero|apple|orange|for the fun of this

"\"A"Hero|apple|mango|lots of fun always

"\"A"Her|apple|fruit|no

pain is the way

"\"A"Hero|love|stackoverflowpeople|more fun

Resulting:

"\"A"Hero|apple|orange|for the fun of this

"\"A"Hero|apple|mango|lots of fun always

"\"A"Her|apple|fruit|no pain is the way

"\"A"Hero|love|stackoverflowpeople|more fun

my problem isnt the finding the if (line.StartsWith(begins)) its the else statement, it appends line2 to a new line


it seems like your string is not well formated...

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

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);
            }

        }

    }


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);
        }
    }

} 

It does have the problem of appending to an existing file, though. I suggest using a StreamWriter rather than 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);
            }
        }
    }
} 


Your problem is that the \ is a C# escape code.
Your string is parsed as "A, because \" is the escape code for a single ".

You should make the begins string an @-string, which does not use escape codes.
You will then need to escape the " by doubling it up. For example:

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

Note that the best way to do this is to use a StreamWriter, like this:

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);
    }
}


Based on @SLaks's example here is some 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号