Need help reading in specific type of file

This is a discussion on Need help reading in specific type of file within the C Programming forums, part of the General Programming Boards category; Does anyone know of a better way (ie one that works) to read in a file called "test.b" containing brookshear ...

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Question Need help reading in specific type of file

    Does anyone know of a better way (ie one that works) to read in a file called "test.b" containing brookshear machine instructions. This is what I have come up with, this compiles but performs an execution error. (file opening works fine, I must be using fscanf wrong).

    FILE *code_file;
    code_file = fopen("test.b", "r");
    if (code_file==NULL) {
    printf("Unable to open file");
    exit(0);
    }
    for (i=0; memory[i]!=0xc0;i+=2) {
    fscanf(code_file, "%x%x", &memory[i], &memory[i+1]);
    }


    I tried only scanning in one hex number in fscanf and increasing i by 1 but got the same error.

    Any help would be great, cheers...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It sounds like you're trying to read a binary file. In that case you want to use fread and fwrite instead of fscanf and fprintf.
    code_file = fopen("test.b", "r");
    should instead be: (to open a binary file)
    Code:
    code_file = fopen("test.b", "rb");
    -Prelude

  3. #3
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Sorry, I meant to post what was in "test.b"

    it just contains two pairs of hex values on each line, which I'd like to read in as two saparate values if possible

    Test.b contains the following, but as this is a test I will need some generic code later to get the file name from the user (can do that myself)

    11AA
    2031
    32AB
    4012
    5312
    7442
    8551
    9634
    BR13
    C000

    Thanks, I'll try your suggestion.

  4. #4
    Unregistered
    Guest
    for (i=0; memory[i]!=0xc0;i+=2) {
    This is just plain nasty, I think a while loop would work better instead of a for loop.

    If you want to read in two separate values, try reading two characters into a string twice per line and then converting it to a hex. Assign the first hex to a variable and do the same thing with the second set of characters.

    And you can tighten up your code a bit where you open the file
    Code:
    FILE *code_file;
    if((code_file=fopen("test.b", "r+"))==0){
        printf("Unable to open file!");
        exit(1);
    }

  5. #5
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Talking

    Figured this out on my own

    It was definitely some nasty code... looks and runs like a gem now... might use your file opening tip though, thanks.

    The entire program is actually designed to run a brookshear machine program... any ideas where I could find an actual program (brookshear) to test my code out properly with?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21