Binary file is read with bytes missing

This is a discussion on Binary file is read with bytes missing within the C# Programming forums, part of the General Programming Boards category; Hi, im a new programmer to C# and I just do not understand why my binary file is read incorrectly. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    Binary file is read with bytes missing

    Hi, im a new programmer to C# and I just do not understand why my binary file is read incorrectly.

    I have a 625 byte binary file and it only reads 586 bytes, skipping a byte once in a while.
    The result is the same everytime.

    Is there something im missing?

    Any help would be truly appriciated.

    Code:
    	//Returns the file data as a byte array
    
    	public static byte[] readfilebytes(string filex)
            {
    
                TextReader tr = new StreamReader(filex);
                string buffer;
                string collected = "";
    
    
                while ((buffer = tr.ReadLine()) != null)
                {
                    collected += buffer;
                }
    
    		
                tr.Close();
                Console.WriteLine(collected.Length);
    	    //625 byte binary file
                //586 bytes in the variable collected
    	
    
                byte[] bytes = new byte[collected.Length];
                int i = 0;
                foreach (char c in collected.ToCharArray())
                {
                    bytes[i] = (byte)c;
                    i++;
                }
    
                return (bytes);
    
            }

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    Perhaps:

    test.txt
    D
    d
    One enter between the two D's is also a byte, but it is not read into the buffer, thus not copied into bytes ( your program will output 2 but filesize will be 3 bytes ) ....

    None of escape characters like \0 ( end of string in C ), \n end of line , \t etc etc are not being taken into account in your program. When you check the filesize these bytes will be taken into account, thus explaining the difference .

    A suggestion: use BinaryReader , .BaseStream.Length to know the length of the stream instead of copying to a buffer first.
    Last edited by GanglyLamb; 09-03-2006 at 05:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 06:31 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21