2009-06-15 2 views
1

입력을 경로로 저장하려고 시도했지만 입력 내용을 읽을 수 없습니다. 임 디버깅 및 그 라인을 완전히 건너 뜁니다!C# 내 입력을 읽지 못함

public static void OpenFile(int FileSize) 
    { 
     char GetLines = ' '; 
     char[] FileContents = new char[FileSize]; 

     Console.WriteLine("Enter a Path Name: "); 
     GetLines = (char)Console.Read(); 
     GetLines = (char)Console.Read(); // Getting No Input Y? 

     StreamReader MyStream = File.OpenText(GetLines.ToString()); 


     while (GetLines != null) 
     { 
      Console.WriteLine(FileContents); 
      GetLines = (char)MyStream.Read(); 
     } 
     MyStream.Close(); 


    } 

다른 모든 기능은 정상적으로 작동합니다. 이 함수는 Main에서 호출 중입니다 ... 내 목표는 여전히 파일의 내용을 배열로 읽으려고 시도합니다.

이것은 직업 분류가 아닙니다! =)

+0

문자 그대로 GetLines는 null이 될 수 없으므로 while 루프가 끝나지 않습니다.컴파일러가 아마 그것에 대한 경고를 발표했습니다. –

답변

3

Console.ReadLine() 및 MyStream.Readline()을 사용하지 않는 이유는 무엇입니까? 당신은 당신이 Console.Read를 사용 하시겠습니까

public class ReadTextFile 
{ 
    public static int Main(string[] args) 
    { 
     Console.Write("Enter a File Path:"); 
     string fileName = Console.Readline(); 
     StreamReader reader = File.OpenText(fileName); 
     string input = reader.ReadLine(); 
     while (input != null) 
     { 
      Console.WriteLine(input); 
      input = reader.ReadLine(); 
     } 
     reader.close; 
     return 0; 
    } 
} 
0

위치 : 여기

는 위해 StreamReader 예입니다? 입력에서 다음 문자를 읽습니다 (1 문자 그대로). 대신 Console.ReadLine을 사용하면 전체 행을 읽습니다. 나는 (두 번째 Console.Read 라인이 GetLines 변수의 내용을 대체합니다) 올바르게 해석 할 경우

또한, GetLines 위의 코드에서, 입력 내용의 두 번째 문자가 포함됩니다.

2

아마도 Console.ReadLine()을 사용해 보시기 바랍니다.

그 자체로 사용자가 입력 한 두 번째 문자를 읽고이를 경로 이름으로 처리합니다. 당신이 문자열에서 파일의 전체 내용이 필요하면

1
public static void OpenFile(){ 
    string path; 
    while(true){ 
     Console.Write("Enter a path name: "); 
     path = Console.ReadLine(); 
     if(File.Exists(path)) 
      break; 
     Console.WriteLine("File not found"); 
    } 

    string line; 
    using(StreamReader stream = File.OpenText(path)) 
     while((line = stream.ReadLine()) != null) 
      Console.WriteLine(line); 
} 

에 함수의 후반 부분을 변경

string file; 
    using(StreamReader stream = File.OpenText(path)) 
     line = stream.ReadToEnd(); 

당신이 정말 필요한 것은 바이트 배열을 사용하는 경우 :

byte[] file; 
    using(FileStream stream = File.OpenRead(path)){ 
     file = new byte[stream.Length]; 
     stream.Read(file, 0, file.Length); 
    } 
0

Console.Readline()이 아마도 필요한 것일 수 있습니다. Console.Read()는 단일 문자를 읽습니다.

또한 while 루프에 문제가 있습니다. char는 값 유형이므로 null이 될 수 없습니다.

0

Console의 documentation을 확인하십시오. 작동 방법을 확인하십시오.

'a'와 'b'를 연속 입력으로 입력한다고 가정 해 보겠습니다. 스트림에 문자가없는 경우 Console.Read 블록 - 사용자가 Enter 키를 누를 때까지 반환되지 않습니다 (OS에 종속 된 행 끝 문자 구분 기호 (Windows의 경우 \ r \ n)를 추가하는 경우) 첫 번째 읽기()의 입력 abc[Enter], GetLines는 A, B를 포함하고 각각 c를 것 대신에 경우의 입력하면 a[Enter]

GetLines = (char)Console.Read(); // blocks till you press enter (since no chars to read) - contains 'a' (97) 
GetLines = (char)Console.Read(); // doesn't block reads \r (13) from the stream 
GetLines = (char)Console.Read(); // doesn't block reads \n (10) from the stream 

을 가정 해 봅시다.

을 다른 사람이 당신은 아마 작성한 Readline (원하는 지적했듯이)하는이 동작 더 직관적으로.

관련 문제