Hi
I'm having a problem reading in a file. I am using BCB6 Personal Edition under Win XP. The file is always read correctly up to the 4413th byte then the data is corrupted. I have modified the code from a fixed array size to use a vector so I have been able to confirm that the correct number of bytes is read in and there is more than sufficient memory available.

My apologies for the formatting, I've tried to reduce the word wrap as much as possible

FileHandler InputBinFile;
AnsiString InputFileName;
unsigned int byteCount=0;
char    tempChar1;        //debug
char    tempChar2;        //debug
unsigned int testSize;        //debug


//get filename
InputFileName = FileOpen1->Dialog->FileName;

//open file to read data in
unsigned int fileErr = InputBinFile.openInputFile(InputFileName);
if(fileErr==0){
    if(!(InputBinFile.isOpen())){    // check file is open
                MapEditorSB->Panels->Items[0]->Text = "Error- No File Open";
	return;
	}

   BinFileSize = InputBinFile.getLength();
   // copy file to array
   InputBinFile.goToBeg();
   DataHexArray.clear();
   testSize = (unsigned int) DataHexArray.max_size();
               
   while(byteCount!=BinFileSize){
                DataHexArray.push_back(InputBinFile.getChar byteCount));
                byteCount++;
                }


   testSize = (unsigned int) DataHexArray.size();
   tempChar1 = DataHexArray[4412];
   tempChar1 = DataHexArray[4413];
   InputBinFile.closeFile();

   // display filename and close file
   MapEditorSB->Panels->Items[0]->Text = InputFileName;
   InputBinFile.closeFile();

   }//end if isOpen
   else{  //filErr==1
     MapEditorSB->Panels->Items[0]->Text = "Error - Can't open file";
                }

        }

The filehandler is a class using fstream it reads a character in using the following method:

unsigned char FileHandler::getChar(unsigned int position)
{
        unsigned char inData;

        FileHandlerFile.seekg(position,ios::beg);
        FileHandlerFile.read(reinterpret_cast<char*>( &inData ),1);    //retrieve one character at a time
        return inData;
}

TIA
James

OMG that must be incredibly sloooooow -- calling seek for each character! A much quicker, and more reliable, solution would be something like this:

unsigned char inData;
while( FileHandlerFile.read((char *)&inData,1) )
    DataHexArray.push_back(inData);

The file is only about 8K so its not noticably slow, but I take your point. I'm a hardware engineer and my programming knowledge is rather limited. The FileHandler class is used several times so the code needs to be generic. I could add another routine to read a file rather than a single character, can I pass a pointer to a vector?
Thanks
James

I think I have part of the answer. I am attempting to read in a binary file. Checking the file the data is corrupted when it gets to an instance of 0x1A, this is the ASCII 'SUB' character. A Google reveals that this caharacter can trigger EOF. I have changed the code that opens the file to:

int FileHandler::openInputFile(AnsiString InputFileName)
{

        FileHandlerFile.open(InputFileName.c_str(), ios::binary);
        if(!FileHandlerFile)              // check file is open
                {
                return 1;
                }
        fileOpen=1;
       return 0;
    }

but with the binary flag set the file isn't opened.

>>but the binary flag set the file isn't opened
Then there is something else wrong, such as incorrect filename passed to that function or the file is not in the directory you think it is in. Add the full path to InputFileName and see if that works.

The pathname is returned by a dialogue box. It works if I specify ios::in and ios::binary, I guess that its just one of those quirks that has to be learnt. Thanks for your help.
Cheers
James

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.