Removing blank line at the end of the Text file.

KK
KK used Ask the Experts™
on
I have a simple log text file with the extension of .txt with a white space line at the end of that text file.

Text File
Code
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
You can use IsNullOrEmpty

if Not String.IsNullOrEmpty(line) then write into the file.

or

you can also use

IsNullOrWhiteSpace
Ryan ChongSoftware Team Lead

Commented:
the problem is the WriteLine statement in your loop which generate the extra line at the last row.
KK

Author

Commented:
I have googled about the same but where i can write that in my code.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

KK

Author

Commented:
@Ryan Chong:I know it but i need the solution.
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Send us the solution in text format. Image is there - I cannot change. :)
KK

Author

Commented:
Please find the code file attachment
Code123.txt
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Pls try this -

temPath = Path.ChangeExtension(sFilePath, ".txt");
using (var output = new StreamWriter(temPath))
{
   foreach(var path in filePaths)
	{
		using (var input = new StreamReader(path))
		{
			string line = string.Empty;
			while ((line = input.ReadLine()) != null)
			{
			   
				line = line.Replace("\"\"", "$#@$").Replace("\"", "").Replace("\t", "");
				if (line.Contains("$#@$"))
					 flag = true;
					 
				if Not String.IsNullOrEmpty(line) AND NOT String.IsNullOrWhiteSpace(line)
					output.WriteLine(line);
			}
		}
	}

Open in new window

Software Team Lead
Commented:
something like this should works

Boolean isFirstLine = true;
temPath = Path.ChangeExtension(sFilePath, ".txt");
                using (var output = new StreamWriter(temPath))
                {
                   foreach(var path in filePaths)
                    {
                        using (var input = new StreamReader(path))
                        {
                            string line = string.Empty;
                            while ((line = input.ReadLine()) != null)
                            {
                               
                                line = line.Replace("\"\"", "$#@$").Replace("\"", "").Replace("\t", "");
                                if (line.Contains("$#@$"))
                                     flag = true;
                                line = Convert.ToString(line);
                                   
								if (isFirstLine)
								{
									isFirstLine = false;
								}
								else
								{
									output.Write(output.NewLine);
								}
								output.Write(line);
                            
                            }
                        }
                    }

Open in new window

KK

Author

Commented:
@Pawan Kumar:I have tried by your approach.but still has the new line at the end,
KK

Author

Commented:
Thanks @Ryan for your quick response.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial