Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Good Morning experts,

As shown in this image:
http://s2.postimage.org/5ed0wl5tl/issue.jpg[^]

I am trying to save the data from a list to a text file. I am successfully righting ti file, but i cannot read from the file as shown in the image. I would really apprieciate all the help and wishing you a Happy new year.

Thanks :)
Posted
Updated 30-Dec-12 22:21pm
v2

Hi the problem is you are checking only once if the tr.readline is null. in your text file after the last item I would believe there is a space
Mr Saliba.
causing it to attempt to read the next one.

Also tr readline takes you to the next line so what to do is the following

Loop through the items but keep track of the index 0 - 3 (1 to 4 if your prefer
C#
int index = 0;
string value;
while((value = tr.Readline()) != null){
if(index == 1){
 stList[counter].firstname = value;
} else if//. . . . all the cases

}

index++;

if(index == 4){
index = 1;
}

}


[Edit]Syntax errors fixed[/Edit]
 
Share this answer
 
v2
Comments
Thomas Daniels 31-Dec-12 4:38am    
Good solution, +5!
Gilmore Sacco 31-Dec-12 4:38am    
hello sir and thx for your quick response. I did understand what are you saying, but the problem is i dont know how many profiles of students does the file contain.
prashant patil 4987 31-Dec-12 4:43am    
5+.... owesome solution
L Viljoen 31-Dec-12 4:45am    
Heres what you can do is , if you have 4 lines per student. then I suggest you can use the Readall lines command

string[] lines = System.IO.File.ReadAllLines("Filepath");
if(lines.count != 0){ //avoid zero devision error
int studentCount = lines.count /4
}

but I would recommadn instead of having diferent lines per block of info is creating comma delimited information

firstname, surname, telephone ect

so you have 1 student per line, then you can simply use a
Regex.Split(line, ",")
to split the information and read the text like that , much xleaner and less prone to error.
Gilmore Sacco 31-Dec-12 4:52am    
thx chonal1171. is it possible to find an example or an article i can read about your solution please?
When you read from a file by calling ReadLine, you take a line of text from the file, return it, and advance the pointer. So when you discard one of those lines each time you go round the loop by reading a line and checking if it is null, you are both getting out of step, and running out of file.
Suppose your file contains
Name1
Surname1
Address1
Preference1
Name2
Surname2
Address2
Preference2
Name3
Surname3
Address3
Preference3
The entry to your loop reads and discards "Name1", then puts "Surname1" into the Name field, "Address1" into teh Surname feild, "Preference1" into the Address field, and "Name2" into the Preference field.

Change the Loop condition:
C#
while (tr.Peek() >= 0)
 
Share this answer
 
Comments
Gilmore Sacco 31-Dec-12 4:44am    
hello OriginalGriff,

I did try your suggestion but still, the the problem persists.
OriginalGriff 31-Dec-12 4:53am    
Is there any chance that you are saving data which may contain newlines? For example, addresses often do - if any single address contains a newline, it will throw out your whole system.

Personally, I would store the data a little differently, either as comma separated values or as XML (either of which has facilities to cope with newlines and so forth).
There is a nice article here:
http://www.codeproject.com/Articles/415732/Reading-and-Writing-CSV-Files-in-Csharp
which would make your job a lot easier!
Gilmore Sacco 31-Dec-12 5:05am    
hello once again. Yes i quite like the idea of comma separation and i implemented it in the writing part as sugessted by Chona1171. now i shall try and read. I dont thing there is a newline command in the variables. It it true i am using it to display them, but not the variables themselves
OriginalGriff 31-Dec-12 5:10am    
Depends where they came from - if at any time they were in a multi-line text box for example, the user can add all teh newlines he wants! :laugh:
Gilmore Sacco 31-Dec-12 5:13am    
is it possible? since a am implementing a command line application

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