That's very handy, Damien. Just yesterday I was working on concatenating text and "," to build a CSV line, then worry about parsing it during input. The Split method is much neater. Does it honour quotation marks if they're used? <br>
<br>I remember the days when CASES (the state govt school database) wrote CSV files without quotation marks, and whenever a comma appeared in a datum (e.g. address), everything went out of sync when the data was read back later!<br>
<br>One tip I'll give my kids is regarding reading files of variable length: when saving the contents of an array, the first thing to write to the file is the number of lines that follow. Then write the lines.<br>When reading back the file, first read the number in the first line and use that to dimension your array and to set the upper limit of the loop that reads the lines of data.<br>
<br>I'm not sure if random files exist in VB2010 as they did in VB6. I suspect they've probably morphed into some massively complex .NET database object that I'll never understand :-[<br><br><br><br><div class="gmail_quote">
On 12 May 2011 09:37, ATKINSON-BUCK, Damien <span dir="ltr"><<a href="mailto:Damien.ATKINSON-BUCK@ivanhoe.com.au">Damien.ATKINSON-BUCK@ivanhoe.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div link="blue" vlink="purple" lang="EN-AU">
<div>
<p class="MsoNormal"><a name="12fe171392d168f9__MailEndCompose"><span style="font-size:11.0pt;color:#1F497D">Mark, thanks for another great tutorial. Building on Marks work might be what you need Trav</span></a></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<div style="border:none;border-bottom:solid windowtext 1.0pt;padding:0cm 0cm 1.0pt 0cm">
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">One of the more common uses of reading a text file is to read various values of a CSV (comma separated values) file, where individual items (think fields of a flat file database)
are read into an array for future processing. Visual Basic has a very nice easy way to do this using the Split function. Imagine that you have the above text file with the following data copied from this week’s Aria charts;</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt"> </span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">1,WE RUN THE NIGHT,DJ Havana Brown,ISL/UMA</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">2,FROM THE MUSIC,The Potbelleez,VIC/UMA</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">3,WHAT HAPPENED TO USJessica Mauboy Feat. Jay Sean,SME</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">4,DANCE WITH ME,Justice Crew Feat. Flo Rida,SME</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">5,FRIDAY TO SUNDAY,Justice Crew,SME</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt">6,MAYBE,Sick Puppies,VIR/EMI</span></p>
<p class="MsoNormal" style="border:none;padding:0cm"><span style="font-size:10.0pt"> </span></p>
</div>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:blue">Dim</span> TextFile <span style="color:blue">As</span> <span style="color:blue">
New</span> System.IO.StreamReader(<span style="color:#A31515">"</span></span><span style="font-size:10.0pt;font-family:"Courier New";color:black">u:\albums.txt</span><span style="font-size:9.5pt;font-family:Consolas;color:#A31515"> "</span><span style="font-size:9.5pt;font-family:Consolas">)</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:blue">Dim</span> strAlbumName <span style="color:blue">As</span>
<span style="color:blue">String</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> strAlbumName = TextFile.ReadLine()</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:blue">Do</span> <span style="color:blue">Until</span> strAlbumName
<span style="color:blue">Is</span> <span style="color:blue">Nothing</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:green">'lstAlbums.Items.Add(strAlbumName)</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:blue">Dim</span> Values() <span style="color:blue">As</span> <span style="color:blue">
String</span> = Split(strAlbumName, <span style="color:#A31515">","</span>)</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> strAlbumName = TextFile.ReadLine()</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:green">' Values(0) now contains first column value,</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:green">' Values(1) contains second column, etc.</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas">
<span style="color:blue">Loop</span></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> TextFile.Close()</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:9.5pt;font-family:Consolas"> TextFile.Dispose()</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">Cheers</span></p>
<div>
<table style="width:522.0pt" border="0" cellpadding="0" cellspacing="0" width="696">
<tbody>
<tr>
<td style="padding:0cm 0cm 0cm 0cm">
<p class="MsoNormal"><b><span style="font-size:10.0pt;color:#1F497D">Damien Atkinson-Buck</span></b><span style="font-size:10.0pt;color:#1F497D"><br>
Member of Academic Staff (Secondary)</span><span style="font-size:11.0pt;color:#1F497D"></span></p>
<div>
<div class="MsoNormal"><span style="font-size:10.0pt;color:#1F497D">
<hr style="width:375.0pt" align="left" size="2" width="500">
</span></div>
</div>
</td>
</tr>
<tr>
<td style="padding:0cm 0cm 0cm 0cm">
<table style="width:522.0pt" border="0" cellpadding="0" cellspacing="0" width="696">
<tbody>
<tr>
<td style="width:123.0pt;padding:0cm 0cm 0cm 0cm" width="164">
<p class="MsoNormal"><a href="http://myivanhoe.net/" target="_blank"><span style="font-size:11.0pt;text-decoration:none"><img src="" alt="Description: Description: cid:image002.jpg@01CA7D93.5FFD22D0" border="0" width="200" height="64"></span></a><span style="color:#1F497D"></span></p>
</td>
<td style="width:3.75pt;padding:0cm 0cm 0cm 0cm" width="5">
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span><span style="color:#1F497D"></span></p>
</td>
<td style="padding:0cm 0cm 0cm 0cm">
<p class="MsoNormal"><span style="font-size:7.5pt;color:#1F497D">PO BOX 91 The Ridgeway, Ivanhoe, Victoria 3079 Australia<br>
Telephone +61 3 9490 3848 Facsimile +61 3 9490 3490<br>
</span><a href="mailto:damien.atkinson-buck@ivanhoe.com.au" target="_blank"><span style="font-size:7.5pt">mailto:damien.atkinson-buck@ivanhoe.com.au</span></a><span style="font-size:7.5pt;color:#1F497D"><br>
</span><a href="http://myivanhoe.net/" target="_blank"><span style="font-size:7.5pt">http://myivanhoe.net</span></a><span style="color:#1F497D"></span></p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span style="font-size:10.0pt" lang="EN-US">From:</span></b><span style="font-size:10.0pt" lang="EN-US"> Travis Parker [mailto:<a href="mailto:Travis.Parker@beaconhills.vic.edu.au" target="_blank">Travis.Parker@beaconhills.vic.edu.au</a>]
<br>
<b>Sent:</b> Thursday, 12 May 2011 9:26 AM</span></p><div class="im"><br>
<b>To:</b> Year 12 Software Development Teachers' Mailing List<br>
</div><b>Subject:</b> Re: [Year 12 SofDev] Reading and Writing text data files in VB2010<p></p>
</div>
</div><div><div></div><div class="h5">
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">Thanks for that Mark. I have been using something similar with my students, particularly as the SAC I have written includes reading from a text file (A prototype
helpdesk, including type of job, computer, priority, etc) and creating a new text file with jobs for the techies in a prioritised order.</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">One thing I was wondering though, does anyone have anything good on reading comma separated text files? All the programs I have gotten my students to create
read them a line at a time, but I would ideally like to see something like this line -
</span>tim.cox, hardware, 12, ps40, 2 <span style="font-size:11.0pt;color:#1F497D">
read into arrays. So I would have a “Name”, “Issue”, “Computer_Number”, “Room_Number”, “Priority” or something like that. I’m fine with the arrays, but how would I read it in word at a time (i.e. Between commas” rather than line at a time?</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">With many thanks for a first year SD newbie……</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D">Trav</span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;color:#1F497D"> </span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt" lang="EN-US">From:</span></b><span style="font-size:10.0pt" lang="EN-US"> <a href="mailto:sofdev-bounces@edulists.com.au" target="_blank">sofdev-bounces@edulists.com.au</a> [mailto:<a href="mailto:sofdev-bounces@edulists.com.au" target="_blank">sofdev-bounces@edulists.com.au</a>]
<b>On Behalf Of </b>Mark KELLY<br>
<b>Sent:</b> Wednesday, 11 May 2011 12:45 PM<br>
<b>To:</b> Year 12 Software Development Teachers' Mailing List<br>
<b>Subject:</b> [Year 12 SofDev] Reading and Writing text data files in VB2010</span></p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Hi, you paragons of pedagogy. I've been putting off teaching my kids how to read and write text data files in VB but having investigated today, it's not really more difficult than the days of VB6 with OPEN "file" FOR INPUT AS 1 ... INPUT
#1, data.<br>
<br>
I've attached an adapted tutorial on reading and writing text files with a task involving reading and writing a preferences file (with solution).<br>
<br>
Once your kids can do data files, it greatly opens the scope for populating and playing with really large arrays and makes loops meaningful.
<br>
<br>
I remember how excited I was when I first discovered text files decades ago - it was like finding mum's car keys. My programming world blossomed from that point.<br clear="all">
<br>
-- <br>
Mark Kelly<br>
Manager of ICT, Reporting, IT Learning Area<br>
McKinnon Secondary College<br>
McKinnon Rd McKinnon 3204, Victoria, Australia<br>
Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253<br>
<a href="mailto:kel@mckinnonsc.vic.edu.au" target="_blank">kel@mckinnonsc.vic.edu.au</a><br>
VCE IT Lecture Notes: <a href="http://vceit.com" target="_blank">http://vceit.com</a><br>
Moderator: IT Applications Edulist<br>
<br>
Want a good time? Call 0112358. Ask for Mr Fibonacci.</p>
</div></div></div>
<br>
<br>
<br>
<br>
Privacy, Virus and Copyright Warning<br>
<br>
The information contained in this electronic message (e-mail), and any files transmitted with it:<br>
<br>
* is intended for the named recipients only. If you have received this in error, please advise the sender and delete it and any copies immediately;<br>
* Any personal information in this email must be used in accordance with the Privacy Act 1988 and this always applies even if it has been sent to you in error.<br>
* represents the views of the sender and does not necessarily represent the views or formal advice of Ivanhoe Grammar School;<br>
* may be subject to Copyright, so no further use should be made of it without the author's permission.<br>
<br>
The School does not represent or warrant that the email or any files attached do not contain errors or are free from computer viruses or other defects nor does it accept responsibility for any loss or damage resulting directly or indirectly from the use of
the email or any attached files.
</div>
<br>_______________________________________________<br>
<a href="http://www.edulists.com.au" target="_blank">http://www.edulists.com.au</a> - FAQ, Subscribe, Unsubscribe<br>
IT Software Development Mailing List kindly supported by<br>
<a href="http://www.vcaa.vic.edu.au" target="_blank">http://www.vcaa.vic.edu.au</a> - Victorian Curriculum and Assessment Authority and<br>
<a href="http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html" target="_blank">http://www.vcaa.vic.edu.au/vce/studies/infotech/softwaredevel3-4.html</a><br>
<a href="http://www.vitta.org.au" target="_blank">http://www.vitta.org.au</a> - VITTA Victorian Information Technology Teachers Association Inc<br></blockquote></div><br><br clear="all"><br>-- <br>Mark Kelly<br>Manager of ICT, Reporting, IT Learning Area<br>
McKinnon Secondary College<br>McKinnon Rd McKinnon 3204, Victoria, Australia<br>Direct line / Voicemail: +613 8520 9085, Fax +613 9578 9253<br><a href="mailto:kel@mckinnonsc.vic.edu.au" target="_blank">kel@mckinnonsc.vic.edu.au</a><br>
VCE IT Lecture Notes: <a href="http://vceit.com" target="_blank">http://vceit.com</a><br>Moderator: IT Applications Edulist<br><br><font>Want a good time? Call 0112358. Ask for Mr </font>Fibonacci.<br>