Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have Controller and Person classes,i need to add data in my list,All data that is added to the Console must be stored in a .txt file.That is, if the user inputs 1 to see the datas, it must be read from the file then displayed, when the user inputs 2 to add new data,it must be read from the file then displayed.
i have already written the Show() function,it's working,but add() function doesn't work,what's false i have?

What I have tried:

class Controller
    {
        public List<Person> all; 
        public void Show()
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            foreach (Person item in all)
            {
                Console.WriteLine(item.name + " " + item.age);
            }
        }
        public void Add()
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            
            Console.WriteLine("Enter new user's data");
            string text = Console.ReadLine();
            
            foreach(Person item in all)
            {
                Console.WriteLine(item.name + " " + item.age);
            }
        }


What is my Mistake in Add() function?

[Serializable]
    class Person
    {
        public string name;
        public int age;
        public Person(string a, int b)
        {
            this.name = a;
            this.age = b;
        }
    }





static void Main(string[] args)
        {
            string path = "C:/Users/User/Desktop/mardik.txt";
            List<Person> mardik = new List<Person>();
            //mardik.Add(new Person("Valod", 20));
            //mardik.Add(new Person("Petros", 23));
            //mardik.Add(new Person("Poghos", 25));
            //mardik.Add(new Person("Hranush", 22));
            //mardik.Add(new Person("Suren", 18));
            //FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            //BinaryFormatter bf = new BinaryFormatter();
            //bf.Serialize(fs, mardik);
            //fs.Close();
            Controller c = new Controller();
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("What do you want to do:\n1.Show all People\n2.Add new person\n3.Delete any person");
                int x = int.Parse(Console.ReadLine());
                switch (x)
                {
                    case 1:
                        c.Show();
                        break;
                    case 2:
                        c.Add();
                        break;
                    
                }
            }
        }
Posted
Updated 15-Feb-18 3:47am
Comments
Richard MacCutchan 15-Feb-18 4:25am    
You forgot to include the code to rewrite the file after the new data has been input. A better structure for your program would be:
Start:
    Read the input file data into the List
    While user select options:
        if 1 then show List
    Else
        if 2 then add new entry
    Else
        if some_other_value the end while loop
If new entries added
    Write entries to new file
End:
Suren97 15-Feb-18 5:49am    
i don't understand, can you show me it as code?
Richard MacCutchan 15-Feb-18 5:52am    
I have given you the pseudo-code, converting that to C# is not difficult.
Suren97 15-Feb-18 5:54am    
but i think i have done like that, or i just don't understand what you mean?
Richard MacCutchan 15-Feb-18 6:08am    
Sorry, but you have not. Look at the structure of your code. You read the input file in two places, so you create two separate lists of objects; that is just duplicating work. And it also means that every time you call Show or Add you re-read the file again, which is even more wasteful. Other issues as listed in OriginalGriff's message below.

Couple of things here:
1) Don't hardcode paths, use a config file instead - and if you can't work that out, then use a const value so you only have to change it in one place.
2) If you open a file or stream, you are responsible for closing and Disposing of it: none of your code does that, which means the stream remains open until either the app closes or the Garbage Collector gets kicked in to clean up after you. If a file is open, you can;t open it for writing!
3) Your add method reads data from the user, but does nothing with it: it is discarded at the end of the method, so there is little point in reading it in the first place!
4) Using the .TXT extension is misleading when you use a BinaryFormatter - it implies human readable and editable content, which a BinaryFormatter does not produce.
5) Never trust user input: int.Parse will throw an exception is the user enters "hello" or "A" instead of a digit. I'd suggest you use int.TryParse instead.

I'd suggest that you look at using blocks around your Streams and Formatters, and that you look at appending the new data from the user to your file as part of you Add method, as well as displaying it in some way.
 
Share this answer
 
v2
Comments
Suren97 15-Feb-18 6:27am    
How can i use config file?
OriginalGriff 15-Feb-18 6:36am    
https://msdn.microsoft.com/en-us/library/ms184658.aspx
Suren97 15-Feb-18 6:30am    
or const value?
OriginalGriff 15-Feb-18 6:37am    
You are kidding, right?

const string myConstValue = "Value of the string";
Suren97 15-Feb-18 6:39am    
for what i should use it?
public void Add()
        {
            const string path = "C:/Users/User/Desktop/mardik.txt";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();
            List<Person> all = bf.Deserialize(fs) as List<Person>;
            fs.Close();
            Console.WriteLine("Enter new user's data with comma");
            string text = Console.ReadLine();
            string[] segments = text.Split(',');
            string a = segments[0];
            int b;
            bool hajoxvec = int.TryParse(segments[1], out b);
            if (!hajoxvec)
            {
                Console.WriteLine("Please try again");
                this.Add();
            }
            else
            {
                all.Add(new Person(a, b));

            }
                foreach (Person item in all)
                {
                    Console.WriteLine(item.name + " " + item.age);
                }
            fs.Close();
        }


it's working, now i need only to save my adding data.
 
Share this answer
 
Comments
Richard MacCutchan 16-Feb-18 11:52am    
It's still wrong.
Suren97 16-Feb-18 11:55am    
what's my mistake?
Richard MacCutchan 16-Feb-18 12:06pm    
You are still doing what I told you not to do. Go back and read my suggestion, and OriginalGriff's.
Suren97 16-Feb-18 14:29pm    
Can you send me any source where i can find the answer of my question?
Richard MacCutchan 17-Feb-18 3:48am    
I have given you the answer; you just will not try and use it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900