1

I want to read a textfile, change a format, and then output it in a different file.

I got the following code to read the original textfile and read it into a different file. How can I change the format?

Original textfile

116

11/2/2012 18:22

N9 45.483 E10 30.495

416 m

117

11/2/2012 18:22

N9 45.483 E10 30.495

415 m

New textfile after changing the format

116,11/2/2012 18:22,N9 45.483 E10 30.495,416 m
117,11/2/2012 18:22,N9 45.483 E10 30.495,415 m

Source code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Reading_textfile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\Users\ryuma\Downloads\Elephantread.txt";

            //string[] Lines = File.ReadAllLines(filePath);

            List<string> lines = new List<string>();
            lines = File.ReadAllLines(filePath).ToList();

            foreach (String line in lines)
            {
                Console.WriteLine(line);
            }

            string filePath2 = @"C:\Users\ryuma\OneDrive\Desktop\WriteFile.txt";
            File.WriteAllLines(filePath2, lines);
            Console.ReadLine();
        }
    }
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
Yoshi
  • 111
  • 6
  • To change the format you should 1) read the parts of the file into variables in C#, 2) perform any transformation necessary (e.g. if the date format changes). 3) write the new file from these variables in the desired format. There's no magic "rewrite my file in a different format" functionality. – ProgrammingLlama Dec 12 '19 at 08:47
  • string.Join(",",lines.ToArray()); – bimal george Dec 12 '19 at 08:59

3 Answers3

0

This should work if you add a reference to System.Linq

        var srcLines = File.ReadAllLines(filePath);

        var text = string.Join(",", File.ReadAllLines(filePath));
        var dstLines = text.Split(new []{" m,", " m"}, StringSplitOptions.RemoveEmptyEntries).Select(x => x + " m");
        string filePath2 = @"C:\Users\ryuma\OneDrive\Desktop\WriteFile.txt";
        File.WriteAllLines(filePath2, destLines);
djsoteric
  • 188
  • 1
  • 10
0

You can read file as text, then it would be easier to manipulate:

var input = File.ReadAllText(filePath);
var output = input.Replace("\r","").Replace("\n\n",",").Replace("m","m\n");
File.WriteAllText(output);

The idea is to replace new lines with , (doing it in system-independend way, otherwise use this solution) and then add new lines after m characters.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

I would suggest doing it in a readable and clean way.

static void ChangeFormatInDestination(string sourcePath, string destinationPath)
    {
        try
        {
            if (File.Exists(sourcePath))
            {
                string[] allLines = File.ReadAllLines(sourcePath);
                // Merge all non-null lines in one line separated by commas
                var convertedSingleLine = string.Join(",", 
                                allLines.Where(s=>!string.IsNullOrEmpty(s)));
                // Replace the " m,"
                convertedSingleLine = convertedSingleLine.Replace(" m,",
                                      " m" + System.Environment.NewLine);

                File.WriteAllText(destinationPath, convertedSingleLine);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"There was an error while manipulating the files. Exception: {ex.Message}");
        }
    }
Indrit Kello
  • 1,293
  • 8
  • 19