Resolved Having a problem with deserializing all data from a txt file.

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
I am able to add multiple patients to the txt file, however when I attempt to read all the data from the file, it only prints one patient.

Secondly, when I add a patient, how do I move it onto the next line? at the moment the patient gets appended onto the same line at the first patient.

Could someone please help me? Thankyou





C#:
            List<User> PInfo = new List<User>();



            if (File.Exists("PInfo.txt"))
            {
                Stream stream = File.Open("PInfo.txt", FileMode.Append);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, patient);
                stream.Close();
                stream.Position = 0;
                Console.Clear();
                DisplayMenu();
            }
            else
            {
                Stream stream = File.Open("PInfo.txt", FileMode.Create);
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(stream, patient);
                stream.Close();
                stream.Position = 0;
                Console.Clear();
                DisplayMenu();
            }
     public static void ViewAllRecords()
        {
       
            string choice;
            object obj = null;
                 
            Stream stream = File.Open("PInfo.txt", FileMode.Open);      
            BinaryFormatter bf = new BinaryFormatter();
            obj = (User)bf.Deserialize(stream);      
            stream.Flush();                            
            stream.Close();
            Console.Clear();
     
            Console.WriteLine(obj.ToString());        
            Console.ReadLine();

     }
     
}
 
Last edited:
You need to debug your code. Set a breakpoint at the top and then step through it line by line. You will then be able to see exactly where and how its behaviour differs from your expectation. Even if you still can't fix the problem for yourself, at least you can provide us with all the relevant information.
Secondly...
Please keep each thread to a single topic and each topic to a single thread. That way we don't multiple conversations crossing each other or one conversation broken across threads. If appropriate, you can ask multiple questions at the same time in separate threads, providing only what's relevant to that question in each thread. Otherwise, you can ask one question first and, when that's answered, ask another if you still need to.
 
Instead of just serializing a single patient and append that to a file, you should serialize and deserialize the entire list.

Anyway, on line 50, you are only deserializing only a single item. Why are you surprised you got only one item?
 

Latest posts

Back
Top Bottom