AlexTheRose
AlexTheRose

Reputation: 135

How to read bytes until a certain character is encountered in C

I'm wanting to read a string of bytes for processing by a character table in C, that is until a character is encountered (0xFF). Basically, I'm creating a function that is supposed to process text out of a static binary file that uses a custom character set into ANSI.

The code I have looks like this:

short ReadText(char * Path, char * Offset) {
    //Declare variables
    size_t ReadResult;
    FILE * OpenedFile;
    short Position;
    char CurrentChar;
    char FormattedChar;
    char * FileContents;

    //Populate variables
    OpenedFile = fopen(Path, "r");

    do { 
        // >>> Code to gather bytes <<<
    }
    //Initiate character conversion
    do {
        fseek(OpenedFile, Position, SEEK_SET);
        CurrentChar = fread(

        //Character switch I/O
        switch(CurrentChar) {enter code here
            case 0x00: FormattedChar = ' ';
            case 0x01: FormattedChar = 'À';
            case 0x02: FormattedChar = 'Á';
            case 0x03: FormattedChar = 'Â';
            // . . .
        }
    }
}

What I need to know how to do is to read bytes from an offset into a char* until the original charset's terminating byte is encountered (which is 0xFF). How can I do that?

Upvotes: 0

Views: 1784

Answers (3)

chux
chux

Reputation: 153338

Suggest handling buffers by declaring the destination buffer, and its size outside ReadText(). Create a simple loop and repeatedly call fgetc() and be sure to put its result in an int to test for EOF and setup the translation. Unless there are only a few bytes to translate, rather than a switch, simple add a 256 byte translation array. Note that int CurrentChar will have a 0-255 value from fgetc().

ssize_t ReadText(const char * Path, char *Dest, size_t DestSize, 
    long *Offset) {
  //Declare variables
  FILE * OpenedFile;
  char FormattedChar;
  size_t i;

  //Populate variables
  // OpenedFile = fopen(Path, "r");
  OpenedFile = fopen(Path, "rb");  // OP read a "binary file"
  if (OpenedFile == NULL)
    return -1;
  if (fseek(OpenedFile, *Offset, SEEK_SET)) {
    fclose(OpenedFile);
    return -1;
  }
  DestSize--; // for a terminating '\0'
  for (i = 0; i < DestSize; i++) {
    int CurrentChar = fgetc(OpenedFile);
    if (CurrentChar == EOF || CurrentChar == 0xFF)
      break;

    // perform translation via a switch if few
    switch (CurrentChar) {
      case 0x00:
        FormattedChar = ' ';
        break;
      case 0x01:
        FormattedChar = 'À';
        break;
        // ...
    }
    // or use a lookup
    static const char XLate[256] =
            { ' ', 'À', 'Á', 'Â' /* and the 252 remaining */};
    FormattedChar = XLate[CurrentChar];
    Dest[i] = FormattedChar;
  }
  fclose(OpenedFile);
  // If OP appears needs a terminating \0
  Dest[i] = '\0';
  *Offset += i;  // Update *Offset for another call if OP desires.
  return i;
}

Upvotes: 0

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

Hi you might be want this,

short ReadText(char * Path, short Offset)
{
    //Declare variables
    size_t ReadResult;
    FILE * OpenedFile;
    short Position;
    int byte;
    unsigned char CurrentChar;
    char FormattedChar;
    char * FileContents;

    //Populate variables
    OpenedFile = fopen(Path, "r");

    if (OpenedFile == NULL)
    {
        printf("---File not exist----\n");
        return;
     }

     fseek(OpenedFile, Offset, SEEK_SET);

     while(1)
     {
         unsigned int number;
         byte = fscanf(OpenedFile,"%x",&number);

         printf("read ::%x\n",number);


        switch(number) 
        {
            case 0x00: FormattedChar = ' ';break;
            case 0x01: FormattedChar = 'À';break;
            case 0x02: FormattedChar = 'Á';break;
            case 0x03: FormattedChar = 'Â';break;
            ....................................
            ....................................
        }

        if(number == 0xFF)
            break;
    }

    fclose(OpenedFile);
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201399

I think you just need to add a call to feof(FILE *) like so,

fseek(OpenedFile, Position, SEEK_SET);
if (feof(OpenedFile) != 0) {
  /* end of the file. */
} else {
  /* safe to read. */
}

Upvotes: 1

Related Questions