I have a txt file looking like this:
1 5 7 5
4 8 19 6
23 56 78 9
I want to read all these values and write them to new file, but with different order.
Output file should look like this:
1 4 23
5 8 56
7 19 78
5 6 9
Right now I am trying to read everything into array, but I am not sure how to handle this data later...
string line;
using (StreamReader sr = new StreamReader(@"C:\Users\as\Desktop\file\1.txt", Encoding.Default))
{
line = sr.ReadToEnd();
string[] lines = line.Split('\n');
string newLine = lines[0].ToString();
}
My input file can have even up to 400000 columns.
EDIT 2:
I tried sth like this, but still not working, any suggestions?
using (StreamReader sr = new StreamReader(@"C:\Users\as\Desktop\files\1.txt", Encoding.Default))
{
List<string> list = new List<string>();
while ((line = sr.ReadLine()) != null)
{
list.Add(line);
}
int valuesNumber = list[0].Split(' ').Count();
List<string> final = new List<string>();
for (int j = 0; j < valuesNumber ; j++)
{
for (int i = 0; i < list.Count; i++)
{
string[] stringArray = list[i].Split(' ');
final .Add(stringArray[j]);
}
}
using (StreamWriter writer = new StreamWriter(@"C:\Users\as\Desktop\files\2.txt", true))
{
foreach (string item in result)
{
writer.WriteLine(item.ToString());
}
}
}
But I still get every number under previous one, like this:
1
4
23
5
8
56
7
19
78
5
6
9