XK8ER
asked on
vb.net - display names from text
hello,
I have a text file with first names each one line by line..
I would like to know how can i display the first 10 records and delete them.
Dim x As Integer
For x = 1 To 10
Call Get_Names(x)
Next x
right now I have two functions one that read the first line and one that deletes the first one..
but doing this it has to open the file 10 times to read an another 10 times for deletes..
I have a text file with first names each one line by line..
I would like to know how can i display the first 10 records and delete them.
Dim x As Integer
For x = 1 To 10
Call Get_Names(x)
Next x
right now I have two functions one that read the first line and one that deletes the first one..
but doing this it has to open the file 10 times to read an another 10 times for deletes..
why not read all the lines, perform our action and then delete the file completely:-
var lines = File.ReadAllLines("FilePath");
var loopCounter = lines.Count()/10 + (lines.Count()%10 > 0 ? 1 : 0);
for(int i=0;i<loopCounter;i++)
{
lines.Take(10).ToList().ForEach(Console.WriteLine);
}
File.Delete("FilePath");
ASKER
no i dont want to read the whole file just first 10 lines then remove them
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you do not want to read all the lines, easily achieved by amending this line to just read 10 lines.
TheFileLines.AddRange(Syst em.IO.File .ReadAllLi nes(FileAd dress))
TheFileLines.AddRange(Syst
call Get_Names(x)
Next
2. another option
Do While x<11
call Get_Names(x)
Loop