Answered Streamreader ... read question......

  • Tuesday, May 08, 2012 5:33 PM
     
     

    I have a text file and I want to see if it is empty or no characters on it??

    thanks....

All Replies

  • Tuesday, May 08, 2012 5:40 PM
     
     

    What have you tried?

    You should be able to just read the first line/byte of the file and see if it's empty.

  • Tuesday, May 08, 2012 5:41 PM
     
     
  • Tuesday, May 08, 2012 6:22 PM
     
     

                            FileInfo StoredFile = new FileInfo(Location);

                            if (StoredFile.Length == 0)
                            {
                                MessageBox.Show("Checked file...", "No nothing Stored...");
                            }

    thats not working???

  • Tuesday, May 08, 2012 6:48 PM
     
      Has Code

    FileInfo solution will not work, empty text file usually has some data so length might return greater than 0. One way is to use StreamReader.Peek method with the seeking of the underlying stream is supported. If -1 is returned and stream can be seeked, then you have empty text file. In this check Peek should be called right after creating StreamReader instance before trying to read anything.

            static void StreamReaderCheck()
            {
                using (StreamReader sr = new StreamReader("empty.txt"))
                {
                    int c = sr.Peek();
    
                    if (sr.BaseStream.CanSeek && c == -1)
                    {
                        Console.WriteLine("empty file...");
                    }
                }
            }




  • Tuesday, May 08, 2012 6:53 PM
     
      Has Code

    hi,

    you can use a simple ReadAllText method:

                string text = File.ReadAllText(@"filePath");
                if (text.Length == 0)
                {
                    //no character is file
                }


    Mitja

  • Tuesday, May 08, 2012 6:59 PM
     
     

    I was taking a look at somethings and came up with this ... it's not the cleanest way but it checks the first line..

       StreamReader test = new StreamReader(File.OpenRead(StoredFile));
        if ("" == (StoredFile = test.ReadLine()))
              {
                    MessageBox.Show("Checking file...", "Nothing Stored...");                   
               }
                test.Close();
                test.Dispose();    

     I have only six lines used is there anyway to put in a loop to check to see if all line are there....

    Thanks           

  • Tuesday, May 08, 2012 7:05 PM
     
      Has Code

    hi,

    you can use a simple ReadAllText method:

                string text = File.ReadAllText(@"filePath");
                if (text.Length == 0)
                {
                    //no character is file
                }
    This is definitely a quick and easy check, but what if the file isn't empty.  What if it is in fact an 8GB file?  This code will blow up with an OOM exception, and even if the file wasn't large enough to do that, you're still forcing yourself to read the entirety of a potentially very large file when you know after reading the first byte that it's not empty.
  • Tuesday, May 08, 2012 7:30 PM
     
     

    thanks....  but  doesnt work .. error can't covert file info to string .. I'm using  Path.GetDirectoryName to get location....

  • Tuesday, May 08, 2012 8:25 PM
     
     

    Just tested it .. sometimes it works, some time it doesnt ??? cant figure out that one???

    Any ideas??

    Thanks

  • Wednesday, May 09, 2012 3:02 AM
     
     Answered
    An empty file and and an empty line are not the same thing. An empty file has zero bytes and so your first try (file.Length) was right for finding that.
     
    An empty line is a line that contains just the end of line marker: carriage return + line feed (\r\n) under windows
     
    So to check if none of the six lines of your file is empty you have to do a ReadAllLines into a string array and check that none of the array elements are empty (have a zero length).
     
    Alternatively you can parse the whole file yourself scanning it character by character and knowing that the sequence \r\n marks the end of a line and see if it is always preceeded by some meaningful character(s). But you have to be careful as some foreign files (unix f.i.) have only \n as line terminators; also the last line is not always terminated etc...
     
    /LM

    Just tested it .. sometimes it works, some time it doesnt ??? cant figure out that one???

    Any ideas??

    Thanks


    /LM