A
A
AdTheBisto2018-11-29 16:44:48
OOP
AdTheBisto, 2018-11-29 16:44:48

C# How to correctly read data into an array from a file using StreamReader?

According to the assignment, there is a base class Human, which has the variables Name, LastName, Age and 3 inheriting classes Student, Teacher, TaxiDriver, which also have their own parameters. It is necessary, reading data from one .txt file, to fill in 3 objects of the student, teacher, and driver, having the following file structure:

Student SOne
{ “Name”: “Andrey”,
“LastName”: “Egorov”,
“studentId”: “123456”
“Age”: “20”};
Teacher TOne
{ “Name”: “Ivan”,
“LastName”: “Crunchy”,
“Subject”: “OOP”,
“Age”: “32”};
TaxiDriver DOne
{ “Name”: “Alexander”,
“LastName”: “Grey”,
“TaxiName”: “777”,
“Age”: “40”};

Where the first value (Name, LastName, Age, etc.) is the name of the variable, and the second value is the data itself.
According to the assignment, it is necessary to use a StreamReader, and fill each object with an array of data (namely, an array, not a list), but I can’t figure out how to read data from a file and fill in 3 different objects of different classes, and even such a structure, please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shai_hulud, 2018-11-29
@AdTheBisto

You read line by line using StreamReader.ReadLine() and regularly get 2 values ​​from each line, discarding all other characters (brackets, commas, colons). How to construct parsing and interpretation of these two values ​​is up to you. But I would make a state machine on 3 states: Reading the header, reading fields, end of record.
P.s. Yes, I see that this is JSON, but this is a trap, the task does not say about it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question