none
streamreader readline stops at tabs RRS feed

  • Question

  • I'm using StreamReader.ReadLine() to read in lines from a tab delimited text file.  I've used this literally hundreds of times with no problems.  However, when I'm reading one particular text file, I'm not getting a complete line.  The first line in the file has 4 columns separated by tabs (2 tabs between third and fourth columns).  The first call to ReadLine() reads the first column.  The second call reads the next 2 columns.  The third call reads the last column.  When I step through in the debugger, I can see the tab after the text of the first column on the first read, the tab between the text of the second and third columns but no tab after the third column on the second read, and the fourth column preceded by 2 tabs on the third read.  Does anyone know why ReadLine() is not reading to the end of the line?  
    Monday, December 9, 2019 2:50 PM

All replies

  • Hello,

    please provide more information. Show us the source code of your ReadLine and the line in the file that is read not as you want.

    Regards, Guido

    Monday, December 9, 2019 2:55 PM
  • Hi lfvoydfv,

    Thank you for posting.

    I did some tests based on your description, but it works fine.

    Could you please provide more information about the text file so we can reproduce the problem?

    I look forward to hearing from you.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 10, 2019 7:52 AM
  • And you are absolutely sure there is not end of the line characters, see ReadLine documentation, in those places?

    Anyway to resolve this the example from your code and possibly file is required to figure out what might be the reason if not unexpected characters at those positions.

    Tuesday, December 10, 2019 10:55 AM
  • You could trying (size should not matter unless the file is extremely large) the following.

    var fileName = "TextFile1.txt";
    string[] linesFromFile = File.ReadAllLines(fileName);
    var separator = "\t";
    var lines = linesFromFile
        .Select(lineData => 
            lineData.Split(separator.ToCharArray()));
    
    foreach (var line in lines)
    {
        if (line.Length == 3)
        {
            Console.WriteLine(string.Join("|", line));
        }
    }
    The string.Join is for examining each lines array elements. 

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 10, 2019 2:35 PM
    Moderator
  • Sounds like maybe the file wasn't generated by a Windows system and therefore EOL is being represented by something other than \r\n and therefore when you view the text file it looks fine but when the reader sees the line it sees the (non-Windows) EOL. \r is common as is just \n. 

    I would recommend you load the problem file into a binary editor and look at the places where it is separating the lines incorrectly. I suspect there is something before/after the tab.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, December 10, 2019 2:55 PM
    Moderator
  • Probably the others, such as Michael Taylor, have hinted at the problem. Except my guess is that the columns have a \r or \n at the end of the column.

    You have probably solved the problem already and have just not replied but if you still need help then one thing you can do is to use VS to look at the data but use the Binary Editor. In VS when you open the file using the file open dialog then there will be a down-arrow to the right of the Open button. Click the down-arrow and select "Open With..." then select Binary Editor (near the bottom). If you do find a \r or \n at the end of the columns and are not sure what to do then tell us what you see.



    Sam Hobbs
    SimpleSamples.Info

    Friday, December 13, 2019 8:21 PM