0

I have a txt file and readline is not working right for my file.

My lines in my code.

open picture

And this is my text in my file.Lines are like this, but my code doesnt understand lines like this.

X02233    52330 DISCHY 8 BLUZ
    std STD     0       0       0       0       0               8698230653909   0.00    
X02237    52337 VALONIA BLUZ  STD STD     0       0       0       0       0               8698230653916   0.00 
X02245    72458 HARMONY 9 BLUZ    STD STD     0       0       0       0       0               8698230653923   0.00

UPDATE :

var text = File.ReadAllText(lblPath.Text);
var lines = text.Split('\n'); //Unix-based newline
var longestLine = lines.OrderByDescending(a => a.Length).First();
var shortestLine = lines.OrderBy(a => a.Length).First();
var orderByShort = lines.OrderBy(a => a.Length);

I get out of memory exception in this code.Above example is only a part of my file.My notepad file is 105 MB.

8
  • What Encoding does the file use? What encoding (if any) do you specify for the File instance? Show the actuall code, rather then images, including the code that opens the file. joelonsoftware.com/articles/Unicode.html Mar 25, 2018 at 14:34
  • What you see in VS is the internal representation. Things like \t represent tab spacing and so on.
    – derloopkat
    Mar 25, 2018 at 14:35
  • @Christopher UTF8 Mar 25, 2018 at 14:37
  • @derloopkat I try it open in my Vs and it shows like in my code how can this be possible :)) Mar 25, 2018 at 14:38
  • 1
    It's not clear what you're asking here; "readline is not working right for my file" and "my code doesnt understand lines like this" aren't very descriptive. Based on your sample input, asdad2a[1] should really be at the end of asdad2a[0], not a separate element. Are you sure there's not a '\n' between DISCHY 8 BLUZ and std STD? TIP: Open the input file in Visual Studio and enable Edit > Advanced > View White Space. TIP: Click the magnifying glass icon next to a string value in the debugger to display it in the Text Visualizer, with tabs and newlines rendered as whitespace. Mar 25, 2018 at 18:20

1 Answer 1

1

You can use File.ReadAllText to read the whole file to a string and then use the Split method to split based on the end of line character your file is using:

   var text = File.ReadAllText(myFilePath);
   var lines = text.Split("\n"); //Unix-based newline

File.ReadAllLines by default uses \r\n sequence for new lines - see documentation:

A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed.

2
  • It works and I understand now my problem.Thank you for answer.But now I have exception 'System.OutOfMemoryException' . Mar 25, 2018 at 14:40
  • How big is your file? Could you post the updated code? Mar 25, 2018 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.