2015-01-21 8 views
0

안녕하세요, 저는 친절하게 할 수있는 방법을 알고 싶었습니다. 디자인이 ASCII로 표시되는 .txt 파일이 있습니다. 콘솔을 통해이 파일을 읽고 char 형식의 배열에 ASCII 문자를 추가하고 싶습니다. 문자열을 사용해야하는 파일을 읽는 것을 어떻게 알 수 있습니까? 나는이 권리를 가지고 있습니까 .. 나는이 방법을 발견하지만 난 문자텍스트 파일 읽기 읽기

class Program 
    { 
     static void Main(string[] args) 
     { 


      string[] lines = System.IO.File.ReadAllLines(@"best.txt"); 
      foreach (string line in lines) 
      { 

       Console.WriteLine("\t" + line); 
      } 

      Console.ReadLine(); 
     } 
    } 
+0

여기에 .txt 파일을 게시 할 수 있습니까? – Sandip

+0

이것은 .txt 파일에있는 것의 예입니다. – user3223680

+0

3 번 질문을 읽었으므로 정확히 무엇을 묻고 있는지 알 수 없습니다. – Dennisch

답변

1

흠을 사용하는 방법을 알고하지 않습니다, 귀하의 질문이 충분히 명확하지 않다 ... 그러나?
텍스트 파일의 모든 문자를 2 차원 배열에 매핑 하시겠습니까? 이 같은
뭔가 :

[0, 0] = "a" 
[0, 1] = "b" 
[0, 2] = "c" 
..<omited>.. 
[4, 0] = "x" 
... 
and so on... 

약간의 테스트 텍스트 파일 :

abcdefghij 
1234567890 
jihgfedcba 
0987654321 
xxxxxxxxxx 
0000000000 
yyyyyyyyyy 
9999999999 
---------- 
!!!!!!!!!! 

C# 코드는 :

static void Main() 
{ 
    String input = File.ReadAllText(@"test.txt"); // read file content 
    input = input.Replace("\r\n", "\n");   // get rid of \r 

    int i = 0, j = 0; 
    string[,] result = new string[10,10];   // hardcoded for testing purposes 
    foreach (var row in input.Split('\n'))   // loop through each row 
    { 
     j = 0; 
     foreach (var col in row.Select(c => c.ToString()).ToArray()) // split to array 
     {               // and loop through each char 

      result[i, j] = col;          // Add the char to the jagged array => result 
      j++; 
     } 
     i++; 
    } 
} 


// EDIT: added some code to print out the result. 
// Print all elements in the 2d array. 
int rowLength = result.GetLength(0); 
int colLength = result.GetLength(1); 

for (int k = 0; k < rowLength; k++) 
{ 
    for (int h = 0; h < colLength; h++) 
    { 
     Console.Write("{0} ", result[k, h]); 
    } 
    Console.Write(Environment.NewLine + Environment.NewLine); 
} 

내가 배열의 크기를 하드 코딩했습니다 이 예제에서.