Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary and Text File (all in one) :( 1

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
Hi all, I am attempting to stream a text file which also contains binary data. This in it’s self is not a problem. The problem is, I cannot keep track of where I am in the file. Hence I am not getting all the binary data.
So I suppose my problem is that I cannot find my position in the file where the binary data finishes . . .
The code I am using at the moment in brief.
Code:
long fileLen = streamReader.BaseStream.Length;
// Read and display lines from the file until the end of 
// the file is reached.
while ((line = streamReader.ReadLine()) != null)
{
   if (!gettingBinaryImage)
     filePositionPointer += line.Length + newline.Length;
   else
      filePositionPointer += line.Length;
}

When I get to the end of the file my filePositionPointer is only a third of the size of fileLen? Grrrr! deep breaths!! lol
Hope this makes sense

Any help appreciated.

Age is a consequence of experience
 
I tweaked my previous example to read the bits. how does this work for you
Code:
using System;
using System.Collections;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"c:\sample.txt";
            Class1 test = new Class1();
            string output = test.TransformFile(fileName);

            Console.WriteLine(output);
            Console.ReadKey();
        }
    }

    class Class1
    {
        private byte[] bits;

        public Class1() 
        {
            this.bits = null;
        } 

        public string TransformFile(string filePath)
        {
            //read data from file into an array list and byte array.  this minimizes the time the file is open.
            StringBuilder toReturn = new StringBuilder();
            ArrayList lines = new ArrayList();
            string line;
            using (StreamReader reader = new StreamReader(filePath))
            {
                this.bits = new BinaryReader(reader.BaseStream).ReadBytes((int)reader.BaseStream.Length);

                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

            //cycle through each item in the array list...
            string beginImage = "CIP3_PREVIEW_IMAGE";
            bool isImage = false;
            int pos = 0;
            for (int i = 0; i < lines.Count; i++)
            {
                //convert to string
                line = (string)lines[i];

                //the string begins an image: write the line to the return value and flag an image is about to begin
                if (line.ToUpper().Trim() == beginImage)
                {
                    toReturn.AppendLine(line);
                    isImage = true;
                }
                //the string is an image: convert, append to return value, and flag the image is done
                else if (isImage)
                {
                    toReturn.AppendLine(this.ConvertBinaryToText(pos, line.Length));
                    isImage = false;
                }
                //just standard text: write to return value
                else
                {
                    toReturn.AppendLine(line);
                }
                pos += line.Length;
            }

            //cycle complete return string value
            return toReturn.ToString();
        }

        private string ConvertBinaryToText(int start, int length)
        {
            byte[] input = new byte[length];
            for (int i = start; i < length; i++)
            {
                input[i] = this.bits[i];
            }

            return FormatHexString(BitConverter.ToString(input));
        }
    }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks very much! I would give you double stars if I could..
Going to have to sit down and digest this :)
Thanks again for all your help! That’s a really good start for me.

Age is a consequence of experience
 
A problem that I have come across while implementing this method is: the total length of
Lines:
Code:
for (int i = 0; i < lines.Count; i++)
{
   //convert to string
   line = (string)lines[i];
   testTotalLinesLength+= line.Length;
   ……….
In the file that I am using, the value of testTotalLinesLength is 490,000 thousand (about)
and the length of the bite array is 17,524,567. this obviously shows that the arrays are out of sink, Any ideas why this might be?


Age is a consequence of experience
 
it may be carriage returns since the file was read 1 line at a time the array doesn't account for these.

try testTotalLinesLength+= line.Length + 1;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
An update! This is now finished.
Problems I found with the hex data was, thousands of null characters. The hex was changed to a byte in one lump. So rather than get an “F” and change to a byte which is a value of 70 in base 10 the program or person who created the file got the value”FF” as Hex which is 255 base 10. Hence this could never be converted back to ASCII because it was never treated in that way. So I took your code and made it fit the problem.
Thanks for your help with this problem. I am sure It would have taken much longer without your help.


Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top