Thread: reading old dos binary files

  1. #1
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350

    reading old dos binary files

    Could someone help me with this code. My goal is to create a simple dos program that can read and display the contents of binary help files. I am not looking to create a hex editor or anything like that.

    #include <stdio.h> // old c stuff
    #include <string.h>

    #define CRTWIDTH 80
    #define CRTLENGTH 20

    int main(int argc, char *argv[])
    {
    FILE *inFile = NULL;
    char fileName[80];
    int loop;
    int lines;
    unsigned char fileBuffer[CRTWIDTH -1];

    if(argc == 1)
    {
    printf("\nUsage [drive]:\\directory\\filename.hlp");
    printf("\n--> ");
    gets(fileName);
    }
    else
    {
    strcpy(fileName,argv[1]);

    // open... test file here fopen(fileName, "rb")

    // reading contents... help here is needed

    while(!feof(inFile))
    {
    if(loop < sizeof fileBuffer)
    fileBuffer[loop++] = (unsigned char)fgetc(inFile);
    else
    {
    for(loop = 0; loop <= sizeof fileBuffer; count++)
    printf("%c", fileBuffer[loop]));
    // this char isn't cutting it
    // blah
    }

    This is returning the binary info but not the way in which a user can read it. any ideas on how to fix this would be greatly appreciated.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    If you were taking the simplistic approach - just printing all the printable characters, then
    Code:
    if ( isprint(fileBuffer[loop]) ) printf("%c", fileBuffer[loop]));
    But the binary data will mean something, and understanding it will produce a better tool.
    Eg, it could be
    - pointers to other sections
    - lengths of paragraphs
    etc etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    yes... yes... I'd like the better tool version How would I go about it, if I don't know the structure of how the data was written?

    I tried the isprint, but it's not what I'm looking for.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hey, now that's alright. Thanks Salem!!
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Oddities reading binary data
    By KingCandyCorn in forum C Programming
    Replies: 6
    Last Post: 02-15-2009, 05:47 PM
  3. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  4. Reading data from a binary file
    By John22 in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 02:00 PM
  5. Opening files in binary mode
    By ammar in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2002, 04:14 PM