question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked DewayneBasnett-7583 commented

Unable to process file

Hi,
Is it too big to process file here,
https://1drv.ms/u/s!Ai8CrEskdewXvk21iZzHnqscSRwt?e=34UcMw

using codes below?

              using (StreamReader sr = new StreamReader(@"e:/inp2.txt", System.Text.Encoding.ASCII))
              {
                  while ((line = sr.ReadLine()) != null)
                  {
                      //if (line.Trim() == "")
                      //{
                      //}
                      //else
                      {
                          //str.Add(line.Trim().Substring(6));
                          str.Add(line);
                          //str.Add((line+"").Substring(5));
                      }
                  }
              }
dotnet-visual-basicwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Based on the code shown the tag should be dotnet-csharp not dotnet-visual-basic.

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

If the file is large, you cannot load it to memory, but often you can process it line-by-line.

For example, the next code counts the number of lines:

 long count = 0;
    
 foreach( string line in File.ReadLines( @"...path...", Encoding.ASCII ) )
 {
    // TODO: process 'line' variable
    // . . .
    ++count;
 }
    
 Console.WriteLine( "Total lines: {0:#,##0}", count );

It only keeps the last line. Depending on your problem, you can also load and process a series of lines.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.