Reputation: 908
I'm trying to read and write from a txt file using C#. At the moment, I'm writing a program that reads the names in a list, shows them to the user, asks for another name, and then adds that one to the list. The reading is fine, but there's some problems with the writing.
The code compiles fine using CSC, and it executes fine, but after I type the name to add and hit enter, I get a window popping up saying
FileIO.exe has encountered a problem and needs to close.
Any idea what the problem is?
using System;
using System.IO;
public class Hello1
{
public static void Main()
{
int lines = File.ReadAllLines("Name.txt").Length;
string[] stringArray = new string[lines + 1];
StreamReader reader = new StreamReader("Name.txt");
for(int i = 1;i <=lines;i++){
stringArray[i-1] = reader.ReadLine();
}
for (int i = 1;i <=stringArray.Length;i++){
Console.WriteLine(stringArray[i-1]);
}
Console.WriteLine("Please enter a name to add to the list.");
stringArray[lines] = Console.ReadLine();
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("Name.txt", true)){
writer.WriteLine(stringArray[lines]);
}
}
}
Upvotes: 1
Views: 308
Reputation: 161831
It's good to ensure that you learn of any exceptions at the top level of a console application:
public class Hello1
{
public static void Main()
{
try
{
// whatever
}
catch (Exception ex)
{
Console.WriteLine("Exception!);
Console.WriteLine(ex.ToString());
}
finally
{
Console.Write("Press ENTER to exit: ");
Console.ReadLine();
}
}
}
This way, you'll know why the application had to close.
Additionally, you need to have your StreamReader
in a using
block.
Upvotes: 1
Reputation: 67918
How about we simplify this a bit:
foreach (var line in File.ReadLines("Name.txt"))
{
Console.WriteLine(line);
}
Console.WriteLine("Please enter a name to add to the list.");
var name = Console.ReadLine();
File.AppendLine("Name.txt", name):
Now you're not dealing with the IO at all because you're leaving it to the framework by leveraging those static methods wholly.
Upvotes: 2
Reputation: 223402
You are getting the exception because you are not closing your reader
, Just place reader.Close();
after reading your file to Array.
Even better is to use using
statement, since StreamReader
uses IDisposable
interface, this would ensure closing of the stream as well as its disposal.
string[] stringArray = new string[lines + 1];
using (StreamReader reader = new StreamReader("Name.txt"))
{
for (int i = 1; i <= lines; i++)
{
stringArray[i - 1] = reader.ReadLine();
}
}
Just a side note:
you are use File.ReadAllLines
just to get Length
???, you can fillup your array like:
string[] stringArray = File.ReadAllLines("Name.txt");
instead of going through StreamReader
.
Upvotes: 2
Reputation: 43023
If you want to read all lines from a file into an array, you can simply use:
string[] lines = File.ReadAllLines("Name.txt");
And use that array.
Upvotes: 0
Reputation: 3290
Use the reader.ReadToEnd function like this and don't forget to close the reader when you finish.:
StreamReader reader = new StreamReader("Name.txt");
string content = reader.ReadToEnd();
reader.Close();
The reason you get that exception is because you don't close the reader after reading. So you can't write to it without first calling the Close(); method.
You can also use the using statement instead of closing it like this:
using (StreamReader reader = new StreamReader("Name.txt")){
string content = reader.ReadToEnd();
};
Upvotes: -2