Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write to a text file in one method and then read from another method?
    primarykey
    data
    text
    <p>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: <code>addbook()</code> and read from here: <code>list_books()</code>. 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.</p> <pre><code>//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. } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload