Reputation: 648
Im trying to save a path into my input varaible, but its not reading my input! Im debugging and its completely skipping the line!
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();
}
Everything else works fine. This function is being called in Main... My Goals is still to try and read the contents of the file into an array.
THIS IS NOT A HOMEWORK ASSIGNMENT! =)
Upvotes: 1
Views: 964
Reputation: 1314
You can achieve your goal using Console.Read(). Read here http://msdn.microsoft.com/en-us/library/system.console.read.aspx.
Upvotes: 0
Reputation: 136673
Check the documentation of Console.Read for how it behaves.
Let's say you want to enter 'a' and 'b' as consecutive inputs. Console.Read blocks if there are no characters in the stream - it doesn't return till the user presses Enter (at which point it adds a OS-dependent end-of-line delimiter (\r\n for Windows).
So let's say you input 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
Instead If you input abc[Enter]
for the first Read(), GetLines would contain a, b and c respectively.
As others have pointed you probably want ReadLine() which behaves more intuitively.
Upvotes: 0
Reputation: 180918
Why don't you just use Console.ReadLine() and MyStream.Readline() ?
Here is a StreamReader example:
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;
}
}
Upvotes: 3
Reputation: 5190
Console.Readline() is probably what you need. Console.Read() reads a single character.
Also, your while loop has an issue. A char will never be null, as it is a value type.
Upvotes: 0
Reputation: 29547
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);
}
If you need the entire contents of the file in a string, change the latter part of the function to:
string file;
using(StreamReader stream = File.OpenText(path))
line = stream.ReadToEnd();
If what you really need is a byte array, use:
byte[] file;
using(FileStream stream = File.OpenRead(path)){
file = new byte[stream.Length];
stream.Read(file, 0, file.Length);
}
Upvotes: 1
Reputation: 8459
You probably want to try Console.ReadLine().
As it is, you are reading the second char that the user enters, and treating that as a path name.
Upvotes: 2
Reputation: 158389
Are you sure that you want to use Console.Read
? It will read the next character from the input (1 character that is). If you instead use Console.ReadLine
it will read a full line.
Also, in your code above GetLines
will contain only the second character of your input, if I interpret it correctly (the second Console.Read
line will replace the contents of the GetLines
variable).
Upvotes: 0