Go through a txt file StreamReader

0

I'm trying to go through a txt file with StreamReader , the problem I have is that I can never get past the first row of my txt file and I do not understand why.

I have dealt with a for and with a while but I only cycle on the first row infinitely.

var file = Request.Files[0]; //
string content = new StreamReader(file.InputStream).ReadToEnd();

               using (var reader = new StreamReader(content))
                {
                    while (reader.Peek() >= 0)
                    {
                        var linea = reader.ReadLine();

                        var a = linea.Substring(0, linea.IndexOf("\n", 0));
                        Extraer(a);


                    }
                }

Could someone tell me what I'm doing wrong or how best is my method?

    
asked by E.Rawrdríguez.Ophanim 16.07.2018 в 22:01
source

3 answers

0

Thank you all for your time but I ended up doing this and it works pretty well for me.

string content = new StreamReader(file.InputStream).ReadToEnd();

string[] lines = content.Split(new char[] {
  '\n'
});
int conx = lines.Length;

foreach(string item in lines) {
  string arow = item;
  if (arow != "") {
    Extraer(arow);
  }

}
    
answered by 18.07.2018 / 16:43
source
1

You could evaluate using ReadLine()

var file = Request.Files[0]; 

using (var reader = new StreamReader(file.InputStream)) 
{
    while (reader.Peek() >= 0) 
    {
        var linea = reader.ReadLine();

        var a = linea.Substring(0, linea.IndexOf("\n", 0));
        Extraer(a);
    }
}

As you will see in this way you can read line by line

    
answered by 16.07.2018 в 22:11
1

Try this:

int counter = 0;  
string line;
// Lee la fila y muestra en pantalla linea por linea.
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"); 
while((line = file.ReadLine()) != null)  {
    System.Console.WriteLine (line);  
    counter++;  
}
file.Close(); 
System.Console.WriteLine("There were {0} lines.", counter); 
// Suspende.
System.Console.ReadLine(); 
    
answered by 18.07.2018 в 16:22