Question

I'm trying to read from a binary file with BinaryReader.

This is how I call my reading method :

         foreach (Movie film in TreeBuilder.myMovies)
        {
            if (File.Exists(@"C:\Users\DaPhunk\Documents\Visual Studio 2010\Projects\YLK\Binary\" + film.MovieName + ".txt"))
            {
                string[] myArray = operationManager.BinaryReading(film.MovieName);
                if (myArray != null)
                {
                    this.textStarring.Text = myArray[1];
                    this.textProduced.Text = myArray[2];
                    this.textMusic.Text = myArray[3];
                    this.textYear.Text = myArray[4];
                    this.textTime.Text = myArray[5];
                }
                else
                {
                    MessageBox.Show("An error occured");
                }
            }

This is my reading method :

                 public string[] BinaryReading(string name)
       {

            FileStream myStream = new FileStream(@"C:\Users\DaPhunk\Documents\Visual Studio 2010\Projects\YLK\Binary\" + name + ".txt", FileMode.Open);
            BinaryReader reader = new BinaryReader(myStream);

            string loadString = reader.ReadString();
            string[] loadArray = loadString.Split(',');

            reader.Close();
            myStream.Close();

            return loadArray;       

       }

Now something within these lines is not working. My file is not empty because since it's only a string I can read whats in the file.

My problem is I'm trying to find whats wrong with a break point but as soon as I get past this line :

         string loadString = reader.ReadString();

My program starts to run again. How is that possible ? This prevents me from checking what's inside my String[] and what are my values after the method call.

Any idea why ? Thanks in advance.


Update :

I tried to move my breakpoint here

                 string[] loadArray = loadString.Split(',');

But my program never stops so it never gets past the previous line.

Also this is my Writting method :

           foreach (Movie film in TreeBuilder.myMovies)
        {
            if (film.MovieName == name)
            {
                FileStream myStream = new FileStream(@"C:\Users\DaPhunk\Documents\Visual Studio 2010\Projects\YLK\Binary\" + film.MovieName + ".txt", FileMode.Create);
                BinaryWriter writer = new BinaryWriter(myStream);

                writer.Write(film.MovieName + "," + starring + "," + produced + "," + music + "," + year + "," + lenght);
                writer.Close();
                myStream.Close();
            }
        }
Was it helpful?

Solution

Your string will be prefixed with a length field. That makes the .txt extension misleading.

Since it 'suddenly started working again' : is it possible you opened (and saved) one of the files with a Text editor?

Also, your current Read and Write methods are not thread-safe, you should enclose the Stream and Reader/Writer objects in using() {} statements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top