1

I am trying to Make a Stream Writer and then write some input from the Console to a file and then Read the Content out. The only problem is I want to do it in two different methods. Write from here: addbook() and read from here: list_books(). But I can't read from that file because the stream reader does not let me access any of my variables or that file to be used again.

//So I am trying to write from the static void addbook method and then read from the static void list_books method

//I want to Write from one static void and then I want  to read them from a different method.

static void addbook()
{
    //Here is where I get my strings that I will write

    Console.Write("Book Title:");
    string title = Console.ReadLine();

    Console.Write("ISBN#:");
    string isbn = Console.ReadLine();

    Console.Write("Author:");
    string author = Console.ReadLine();

    Console.Write("Publish Date:");
    string publish_date = Console.ReadLine();

    //Here Is where I create the Stream Reader and Writer
    //And where I write to the file
    var fs = File.Open("Librarybooks.txt", FileMode.OpenOrCreate,FileAccess.ReadWrite);
    var sw = new StreamWriter(fs);
    var sr = new StreamReader(fs);

    sw.WriteLine(title.ToCharArray());
    sw.WriteLine(isbn.ToCharArray());
    sw.WriteLine(author.ToCharArray());
    sw.WriteLine(publish_date.ToCharArray());

    Console.WriteLine("Book added Successfully!!");
}

static void list_books()
{
    //Here is where I want to read from so I can just call the list_books method.
    //But I can't access the StreamReader or StreamWriter
    //I just want to be able to read from the file.
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

2

MSDN provides examples of how to do both things

As you can see from he examples, you don't need to convert strings into arrays of characters to write them.

You will also notice both example using the using keyword. There's an explanation of why you want to use that construct in this question (and probably many others): When should I use “using” blocks in C#?

It looks like you already fond the File class, you could take advantage of the other methods in it that are specifically designed for working with text files. You can decide if you want to write to/read from a file in terms of lines or entire contents. There are examples of how to use the File class here:


Based on the code it looks like you're just starting out with programming, consider putting your code up CodeReview. That way you can get helpful tips about how to write better code.

Community
  • 1
  • 1
Roman
  • 19,581
  • 6
  • 68
  • 84
1
private const string FILE_PATH = "Librarybooks.txt";

static void addbook()
{
    //Here is where I get my strings that I will write

    Console.Write("Book Title:");
    string title = Console.ReadLine();

    Console.Write("ISBN#:");
    string isbn = Console.ReadLine();

    Console.Write("Author:");
    string author = Console.ReadLine();

    Console.Write("Publish Date:");
    string publish_date = Console.ReadLine();

    //Here Is where I create the Stream Reader and Writer
    //And where I write to the file
    using (var fs = File.Open(FILE_PATH, FileMode.OpenOrCreate,FileAccess.ReadWrite))
    {
        using (var sw = new StreamWriter(fs))
        {
            using (var sr = new StreamReader(fs))
            {
                sw.WriteLine(title.ToCharArray());
                sw.WriteLine(isbn.ToCharArray());
                sw.WriteLine(author.ToCharArray());
                sw.WriteLine(publish_date.ToCharArray());

                Console.WriteLine("Book added Successfully!!");
             }
         }
     }
}

static void list_books()
{
    using (StreamReader sr = new StreamReader(FILE_PATH))
    {
       string fileContent = sr.ReadToEnd();
    }
}
will simmons
  • 189
  • 2
  • So I did that and then it gives this error: "The process cannot access the file 'C:\Users\Kyson Gardner\documents\visual studio 2010\Projects\Library\Library\bin\Debug\Librarybooks.txt' because it is being used by another process." How can I fix this? – Kyson Gardner Jan 29 '13 at 14:54
  • You did not close the filestream or rather I did not in the code. I updated the answer. Please try it. – will simmons Jan 31 '13 at 14:42