Click here to Skip to main content
11,718,589 members (85,842 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#
Good Morning experts,

As shown in this image:[^]

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 Smile | :)
Posted 30-Dec-12 22:19pm
Edited 30-Dec-12 22:21pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
int index = 0;
string value;
while((value = tr.Readline()) != null){
if(index == 1){
 stList[counter].firstname = value;
} else if//. . . . all the cases

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

[Edit]Syntax errors fixed[/Edit]
ProgramFOX at 31-Dec-12 4:38am
Good solution, +5!
Gilmore Sacco at 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 at 31-Dec-12 4:43am
5+.... owesome solution
Chona1171 at 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 at 31-Dec-12 4:52am
thx chonal1171. is it possible to find an example or an article i can read about your solution please?
Gilmore Sacco at 31-Dec-12 4:48am
hello again, i read the solution multiple times, i have an idea of what you are saying but still i cannot quite understand it. Can you please explain it again to me?
Chona1171 at 31-Dec-12 7:58am
Ok lets say in your text file you have the following file in "c:\myfile.txt":
1, John Smith,054 654 1234
2, Jane Smith,054 654 1235
3, Andrew Smith, 054 654 1236

This is comma delimited data in the form: id, fullname, phone number

now you wish to break it down so in C# you go
string filename = @"C:\myfile.txt";
string[] fileLines = System.IO.File.ReadAllLines(filename);
foreach(string line in fileLines){
string[] dataBreakdown = System.Text.RegularExpressions.Regex.Split(line,","); //seperating the comma delimited data
Console.WriteLine("ID:"+dataBreakdown[0]+" Full Name: "+dataBreakdown[1] +" Tel: "+dataBreakdown[2];
Gilmore Sacco at 31-Dec-12 9:40am
I tried this method and it worked great for the purpose you made it. My original purpose is a liitle different. I have iploaded a screenshot. If you can help me it would be fantastic, as i feel very close to finally make it. Thank you very much :)
Chona1171 at 2-Jan-13 0:14am
I looked at the error, can you please show me the format of your text file becuase something tells me its not comma dlimited or place a line

if(line.Length > 3){
string[] dataBreakdown = System.Text.RegularExpressions.Regex.Split(line,","); //seperating the comma delimited data Console.WriteLine("ID:"+dataBreakdown[0]+" Full Name: "+dataBreakdown[1] +" Tel: "+dataBreakdown[2];
after the foreach string
sariqkhan at 31-Dec-12 8:12am
can you help me sir fox
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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:
while (tr.Peek() >= 0)
Gilmore Sacco at 31-Dec-12 4:44am
hello OriginalGriff,

I did try your suggestion but still, the the problem persists.
OriginalGriff at 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:
which would make your job a lot easier!
Gilmore Sacco at 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 at 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 at 31-Dec-12 5:13am
is it possible? since a am implementing a command line application
OriginalGriff at 31-Dec-12 5:24am
Without knowing what code you use to read the data and insert it into the class fields I cannot say! :)
sariqkhan at 31-Dec-12 8:12am
can you help me sir

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 681
1 OriginalGriff 225
2 F-ES Sitecore 215
3 Jochen Arndt 160
4 Maciej Los 130
0 Sergey Alexandrovich Kryukov 1,679
1 OriginalGriff 610
2 Maciej Los 529
3 Richard MacCutchan 460
4 F-ES Sitecore 455

Advertise | Privacy | Mobile
Web02 | 2.8.150901.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100