Start Reading at the Beginning of a File Again in C

  • Remove From My Forums

 locked

Reading the file from start again!

  • Question

  • Hello everyone!
    Hither is the situation: Im developing a program which reads bunch of files. Ane of them has a special criteria lets say information technology has "hello" in i of it's lines.

    I take one arraylist ( call information technology file_names), with a loop im going through each chemical element of information technology. If it sees a file with "how-do-you-do"in it , I put the name of the file in a cord and a flag which indicates we accept a file like that.

    Now if I read the file afterwards that loop it starts once more from a line after the "hi".
    Here is the lawmaking:

    for ( int j = 0; j < file_names.Count; j++)
    {

                                    Organisation.IO.StreamReader file = new System.IO.StreamReader(Convert.ToString(file_names[j]));
                    while ((line = file.ReadLine()) != nada)
                    {

                        Regex cheque = new Regex("(hi)");
                        bool check_check = bank check.IsMatch(line);
                         if (check_check)
                        {
                            hi_flag = one;
                            file_name= Convert.ToString(file_names[j]);

                                                 }
                        continue;
                    }

    }

                        if (hi_flag == 1)
                        {
                                //!!!Problem!!!////
                                //If i read the file with "hi" in it, information technology starts from the line after "hullo"
                                //How i tin read the file from outset of it?

                        }

    Thanks

    • Edited by Monday, June 30, 2008 ix:15 PM grammer

Answers

  •  Really, on further reading information technology looks like you'll want to call Seek on the base of operations stream along with DiscardBufferedData.

    i.e.

    if  (hi_flag == 1)
    {
        file.BaseStream.Seek(0, SeekOrigin.Begin);// sets the pointer back to the commencement
        file.DiscardBufferedData();// clears out any cached data from the previous reads so that it tin commencement reading from the get-go again
    }
    • Edited past c_jensen Tuesday, July 1, 2008 4:24 PM Formatted code
    • Marked as reply by jack 321 Friday, July 4, 2008 2:36 AM

tschidaglearand1973.blogspot.com

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/b691dc9f-a709-44aa-882c-d734fb0dd763/reading-the-file-from-start-again?forum=csharpgeneral

0 Response to "Start Reading at the Beginning of a File Again in C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel