Rearrange columns into a txt file c#

I have multiple columns and I want to reordered into a txt file, so far the first column is inserted right but I want the next ones to be on the side, they keep writing at the end. This is what I have so far

StreamReader sr = new StreamReader(file.FullName, System.Text.Encoding.GetEncoding("iso-8859-1"));
List <Campos> listaEsquemaEntrada = LeerXML(RutaArchivoConfig);
var lista = File.ReadAllLines(file.FullName);           
string Separador = ObtenerSeparador(RutaArchivoConfig);
int filaInicial = ObtenerLineaInicial(RutaArchivoConfig);
List<string> ListaFinal = new List<string>();
bool PrimeraIteracion = true;
foreach (Campos campos in listaEsquemaEntrada)
{
    if (campos.columna != 99)
    {
        using (System.IO.StreamWriter ArchivoSalida =
                new System.IO.StreamWriter(RutaOrigen + "\\" + "nuevoArchivo.txt",true))
        {
            if (PrimeraIteracion)
            {
                foreach (string line in lista)
                {
                    ArchivoSalida.WriteLine(line.Split(Char.Parse(Separador))[campos.columna - 1]);
                }
                PrimeraIteracion=false;
            }
            else
            {
                foreach (string line in lista)
                {
                    ArchivoSalida.WriteLine(Separador + line.Split(Char.Parse(Separador))[campos.columna - 1]);
                }
            }
           
        }
    }
    else
    {
        continue;
    }
}

But i keep getting this

C�digo de la Cuenta Contable 
111100101   
111150012   
111150016   
111150028   
111150039   
116150101   
116150101   
|Tipo de Comprobante 
|APE
|APE

i want something like this :

C�digo de la Cuenta Contable |Tipo de Comprobante
111100101   |APE
111150012   |APE
111150016   |APE
111150028   |APE
111150039   |APE
116150101   |APE
116150101   |APE

Upvotes: 0

Views: 462

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

Yep, that's how text files work. If you've written a single column to a text file:

var first = new [] { "header", "line1", "line2", "line3" };
File.WriteAllLines(@"c:\temp\file.txt", first);

And you want to write another column

var second = new [] { "header2", "newline1", "newline2", "newline3" };

It's probably simplest to just read the entire file, add each new bit onto the end of each line, and write the whole file again:

var file = File.ReadAllLines(@"c:\temp\file.txt");
for(int i = 0; i < file.Length; i++)
  file[i] += '\t' + second[i]; //add the second column after the first, with a tab

File.WriteAllLines(@"c:\temp\file.txt", file);

Of course, if you have both first and second (as Lists, arrays, IEnumerable<string> etc) before you even write the file the first time, just do the combine then; no need to re-read the file. I just put that in if you're looking to have a process that modifies an existing file written before (eg yesterday, when you didn't have the second column of data)

If your file is massive, you can get more involved with having a gradual read of one file (see File.ReadLines instead), and a gradual write of another file (StreamWriter etc), so you aren't buffering the whole thing into memory.. But if youre talking about a file that is less than, say, 10 megabytes, go for the simple approach and just read it all, modify, write it all

Upvotes: 1

Related Questions