tags:

views:

191

answers:

2

I am reading in a file with multiple lines of data like this:

:100093000202C4C0E0E57FB40005D0E0020C03B463
:1000A3000105D0E0022803B40205D0E0027C03027C
:1000B30002E3C0E0E57FB40005D0E0020C0BB4011D

I am reading in values byte by byte and storing them in an array.

    fscanf_s(in_file,"%c", &sc);  // start code
    fscanf_s(in_file,"%2X", &iByte_Count); // byte count
    fscanf_s(in_file,"%4X", &iAddr);    // 2 byte address 
    fscanf_s(in_file,"%2X", &iRec_Type);  // record type 

    for(int i=0; i<iByte_Count; i++)
    {
        fscanf_s(in_file,"%2X", &iData[i]);
        iArray[(iMaskedAddr/16)][iMaskedNumMove+3+i]=iData[i];
    }
    fscanf_s(in_file,"%2X", &iCkS);

This is working great except when I get to the end of the first line. I need this to repeat until I get to the end of the file but when I put this in a loop it craps out.
Can I force the position to the begining of the next line?
I know I can use a stream and all that but I am dealing with this method.
Thanks for the help

A: 

Your topic clear states that you are using C++ so, if I may, I suggest you use the correct STL stream manipulators.

To read line-by-line, you can use ifstream::getline. But again, you are not reading the file line by line, you are reading it field by field. So, you should try using ifstream::read, which lets you choose the amount of bytes to read from the stream.

UPDATE:

While doing an unrelated search over the net, I found out about a library called IOF which may help you with this task. Check it out.

Bruno Brant
+2  A: 

My suggestion is to dump fscanf_s and use either fgets or std::getline.

That said, your issue is handling the newlines, and the next beginning of record token, the ':'.

One method is to use fscanf_s("%c") until the ':' character is read or the end of file is reached:

char start_of_record;
do
{
  fscanf_s(infile, "%c", &start_of_record);
} while (!feof(infile) && (start_of_record != ':'));
// Now process the header....

The data the OP is reading is a standard format for transmitting binary data, usually for downloading into Flash Memories and EPROMs.

Thomas Matthews