-1
I created a program that performs CRUD , whenever I create, update or delete an employee it updates the Txt file using Streamwriter, when starting the program I am trying to read that same file and already insert in the List I created, to be able to have the data from when I used it earlier, however, the way I did the Streamreader it is not putting in the List correctly, despite reading all the lines.
List<Funcionarios> func = new List<Funcionarios>();
using (StreamReader sr = new StreamReader(@"C:\Users\Usuário\Teste.txt"))
{
string line;
Funcionarios funcionario = new Funcionarios();
while ((line = sr.ReadLine()) != null)
{
funcionario.nome = sr.ReadLine();
funcionario.matricula = sr.ReadLine();
funcionario.cargo = sr.ReadLine();
func.Add(funcionario);
}
There is the part of the code that reads the file and should store in List func.
tries to instantiate the
funcionario
within the while instruction– Walter Felipe
It wasn’t yet, when I saved something inside the file. txt and choose the option to List in the program appears only the position and the employee registration, oq n makes sense already q I put 3 information and in the code I am reading the three..
– Furios G
What exactly do you mean, "he’s not putting it in the list correctly". Present an example of your text file... Instead of putting each information on a line, wouldn’t it be more appropriate to write one line per employee and separate the values by some marker? ex.:
;
– Leandro Angelo
it makes no sense for you to use this form of writing in the document (because sr.readline() is also being a search parameter). Ideally you would register an entire row using specific tabs (string1 - string2 - string3). And when using readline(), make a split to place these strings in each parameter of the object
– Walter Felipe
http://prntscr.com/p0twqb. This was the official I saved. http://prntscr.com/p0tx1m he was saved this way in the archive. txt but when opening the program again and asking it to list, he listed this: http://prntscr.com/p0txfj
– Furios G
Walter how would I do it this way? Using the split? I switched to put all the employee data on one line, and separated the data by ";" I just couldn’t get the Streamreader to store on each object
– Furios G
@Furiosg Edit the question and present your updated code
– Leandro Angelo
Thank you so much for your help, I researched Split and got my code the way I wanted it.
– Furios G