0

次のようなtxtファイルがあります。

1 5 7 5
4 8 19 6
23 56 78 9

これらの値をすべて読み取って新しいファイルに書き込みたいのですが、順序が異なります。

出力ファイルは次のようになります。

1 4 23
5 8 56
7 19 78
5 6 9

現在、すべてを配列に読み込もうとしていますが、後でこのデータを処理する方法がわかりません...

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

私の入力ファイルには、最大 400000 列まで含めることができます。

編集2:

このように sth を試しましたが、まだ機能していません。何か提案はありますか?

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

しかし、次のように、前の番号の下にあるすべての番号を取得します。

1
4
23
5 
8
56
7
19
78
5
6
9
4

6 に答える 6

0

すべての行の長さが同じであると仮定します (この例では、行ごとに 4 つの要素):

        using (StreamReader sr = new StreamReader(@"C:\Users\as\Desktop\file\1.txt", Encoding.Default))
        {
            string content = sr.ReadToEnd();
            string[] lines = content.Split('\n');

            string[][] matrix = new string[lines.Length][];
            for (int i = 0; i < lines.Length; i++)
            {
                matrix[i] = lines[i].Split(' ');
            }

            //print column by column
            var rowLength = matrix[0].Length; // assuming every row has the same length

            //for each column
            for (int i = 0; i < rowLength; i++)
            {
                //print each cell
                for (int j = 0; j < matrix.Length; j++)
                {
                    Console.WriteLine(matrix[j][i]);
                }
            }
        }
于 2013-11-04T14:34:24.953 に答える
0

私はコードを公開することをあまり信じていないので、疑似コードをいくつか示します。

//define rows as a two dimensional array.
rows := [][]

reader := read_file("1.txt")

pointer:= 0

for each line in the file
    row := line.Split(" ") // So put each number into it's own array slow.
    rows[pointer] := row.
    pointer := pointer +1 // Increase pointer.

すべての行のリストを取得したので、それらから列を作成します。

newRows := [][]

for x = 0 to rows.length
    for y = 0 to rows[x].length
        // Cycle through all values in multidimensional array.
        newRows[y][x] := rows[x][y] 

次に、各値をファイルに出力できます。

于 2013-11-04T14:34:40.563 に答える
0

簡単に引用すると、行を配列に読み込みます。次に、各行を配列に分割する必要があります。あなたの例からの分離は空のスペースのように見えるので、行が次の場合:

1 5 7 5

それを配列に分割する必要があります。

var currentLineColumn = currentLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

したがって、配列は次のようになります。

1
5
7
5

各行で同じことを行います....

于 2013-11-04T14:26:26.280 に答える
0

メモリが問題にならない場合は、line.Split(' ')各行を配列に変換してから、Transpose配列の配列を回転させるメソッドを使用できます。

それは次のようになります。

using (StreamReader sr = new StreamReader(@"C:\Users\as\Desktop\file\1.txt"))
{
    string allLinesAsString = sr.ReadToEnd();
    string[] lines = allLinesAsString.Split('\n');
    string[][] allLines = lines.Select(line => line.Trim().Split(' ')).ToArray();
    var rotatedLines = allLines.Transpose().ToList();
    string rotatedLinesAsString = string.Join(Environment.NewLine,
                                rotatedLines.Select(x => string.Join(" ", x)));
    // write rotatedLinesAsString to a file
}

ファイルが大きすぎてすべてをメモリ内で実行できない場合はFileStream、下位レベルで s を操作するコードを記述できます。これは、ファイルの 2 回の読み取りと多くのシークを意味しますが、メモリ要件は実質的にゼロになります。擬似コードでは、次のようになります。

file_stream = open(input_file)
line_indexes = file_stream.indexes_of('\n')
output_stream = (output file)
loop
    output_stream.write(next item from each line_index)
    move each line_index past the just-read item
end when you reach the end of the line
于 2013-11-04T14:46:03.310 に答える