Jump to content
  • Advertisement
Sign in to follow this  
Renegadeandy

[.net] Carriage Return

This topic is 4004 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right here is the problem. I need to detect if a newline character is found at the end of my file. The file looks like this : 1,pencil,planet office,0.05,1,20 10,black pen,ribo,0.1,5,15 11,blue pen,ribo,0.1,6,8 12,red pen,ribo,0.12,8,3 1015,refill pad (lined),planet office,1,0,50 1016,refill pad (blank),pads are us,1.2,2,5 Its a csv file, so am reading char by char till a "," is found. I get to 20 at the end of the first line, then need to detect the carriage return. A portion of example code is :
  If count = 5 Then
            character = ChrW(inputStream.Read())
            While character <> ControlChars.CrLf


                stringtotal = stringtotal + character
                character = ChrW(inputStream.Read())
            End While

            tempcount6 = CurrentStockLevel.Items.Count

            If tempcount6 = 0 Then

                CurrentStockLevel.Items.Insert(tempcount6, stringtotal)

            ElseIf tempcount6 <> 0 Then

                CurrentStockLevel.Items.Insert(tempcount6 + 1, stringtotal)

            End If

        End If

        inputStream.Close()
notice while character <> ControlChars.crlf that was my attempt, but this isnt a character -its a strng and this is my dilema, same with vbNewLine etc - what other options do i have! Please help, Andy

Share this post


Link to post
Share on other sites
Advertisement
Is there a reason you're parsing it this way? You could just call ReadLine() and process each line until you reach the end of the file or a pre-set number of lines. For the comma separated values you could call string.Split() to get a list of those.

// reader is a System.IO.StreamReader
string line;

while (!reader.EndOfStream)
{
if (!string.IsNullOrEmpty(line = reader.ReadLine()))
{
string[] values = line.Split(',');

// array length check if necessary

// do something with values[0]
// do something with values[1] ...
}
}

Apologies about the C# source. I don't know VB.NET that well but all of this should be interchangeable.

Share this post


Link to post
Share on other sites
Brilliant! Got this as working code:

While inputStream.EndOfStream <> True

line = inputStream.ReadLine

values = line.Split(",")

StockCode.Items.Insert(StockCode.Items.Count, values(0))
ItemName.Items.Insert(ItemName.Items.Count, values(1))
SupplierName.Items.Insert(SupplierName.Items.Count, values(2))
UnitCost.Items.Insert(UnitCost.Items.Count, values(3))
NumberRequired.Items.Insert(NumberRequired.Items.Count, values(4))
CurrentStockLevel.Items.Insert(CurrentStockLevel.Items.Count, values(5))

End While

Share this post


Link to post
Share on other sites

While Not inputStream.EndOfStream
line = inputStream.ReadLine

values = line.Split(",")

StockCode.Items.Insert(StockCode.Items.Count, values(0))
ItemName.Items.Insert(ItemName.Items.Count, values(1))
SupplierName.Items.Insert(SupplierName.Items.Count, values(2))
UnitCost.Items.Insert(UnitCost.Items.Count, values(3))
NumberRequired.Items.Insert(NumberRequired.Items.Count, values(4))
CurrentStockLevel.Items.Insert(CurrentStockLevel.Items.Count, values(5))
End While


That would be very slightly neater (and it's more easily understood, IMHO, than <> for not-equal). It's a very minor change, though.

Share this post


Link to post
Share on other sites
A minor, terser, variation:

For Each Line As String In File.ReadAllLines("file.csv")

Values = Line.Split(",")

' ...

Next


Some of the static methods in File are rather handy. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!