Created
April 20, 2010 00:21
-
-
Save rtipton/371838 to your computer and use it in GitHub Desktop.
C# -- Read from a text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.IO; | |
namespace ReadTextFile | |
{ | |
class ReadText | |
{ | |
static void Main(string[] args) | |
{ | |
//- Location of the Text file | |
string fileName = @"c:temptxt_test.TXT"; | |
//- Set the line counter | |
int lineNumber = 0; | |
//- Write initial text to the console | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | |
Console.WriteLine("MY FAVORITE SONGS"); | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | |
//- Open the text file | |
using (StreamReader sr = new StreamReader(fileName)) | |
{ | |
//- Initialize name variables | |
string aName = ""; | |
string sName = ""; | |
string alName = ""; | |
string line; //- Holds the entire line | |
//- Cycle thru the text file 1 line at a time pulling | |
//- substrings into the variables initialized above | |
while ((line = sr.ReadLine()) != null) | |
{ | |
lineNumber++; | |
//- Pulling substrings. If there is a problem | |
//- with the start index and/or the length values | |
//- an exception is thrown | |
try | |
{ | |
aName = line.Substring(0,21).Trim(); | |
sName = line.Substring(22,25).Trim(); | |
alName = line.Substring(47,17).Trim(); | |
} | |
catch (Exception ex) | |
{ | |
Console.Write(ex.ToString()); | |
} | |
//- Write the information to the console | |
Console.WriteLine("Artist: {0}", aName); | |
Console.WriteLine("Title : {0}", sName); | |
Console.WriteLine("Album : {0}", alName); | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | |
} | |
sr.Close(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.IO | |
Module ReadText_vb | |
Sub Main() | |
'- Location of the Text file | |
Dim fileName As String = "c:temptxt_test.TXT" | |
'- Set the line counter | |
Dim lineNumber As Integer = 0 | |
'- Write initial text to the console | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") | |
Console.WriteLine("MY FAVORITE SONGS") | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") | |
'- Open the text file/stream | |
Dim sr As StreamReader = New StreamReader(fileName) | |
'- Initialize name variables | |
Dim aName As String = "" | |
Dim sName As String = "" | |
Dim alName As String = "" | |
Dim line As String = "" | |
'- Cycle thru the text file 1 line at a time pulling | |
'- substrings into the variables initialized above | |
Do | |
line = sr.ReadLine() | |
'- If it goes past the last line of the file, | |
'- it drops out of the loop | |
If line <> Nothing Then | |
Try | |
aName = line.Substring(0, 21).Trim() | |
sName = line.Substring(22, 25).Trim() | |
alName = line.Substring(47, 17).Trim() | |
Catch ex As Exception | |
Console.Write(ex.ToString()) | |
End Try | |
'- Write the information to the console | |
Console.WriteLine("Artist: {0}", aName) | |
Console.WriteLine("Title : {0}", sName) | |
Console.WriteLine("Album : {0}", alName) | |
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") | |
End If | |
Loop Until line Is Nothing | |
'- Close the file/stream | |
sr.Close() | |
End Sub | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very Nice !!!