Thread: Scanning through a file in C

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    6

    Scanning through a file in C

    Hi,

    I am having trouble with a program I am working on. The program is a disassembler that reads through a hex file and converts it to assembly code. I am using a while loop to loop through the file but it is terminating randomly in the file before it reaches the end.

    Here is my code:

    Code:
    void printAssembly(char fileName[])
    {
        int inst = 0;
        int pc = 0;
        FILE *f;
        char line[2048];
    
        f = fopen(fileName, "r");
    
        fscanf(f, "%x", &pc);
    
       // while((read = getline(&line, &buf, f)) != -1)
       // while((fscanf(f, "%x", &inst) != EOF))
        while(fgets(line, sizeof(line), f))
        {
        //    if(index == 0)
        //    {
        //        pc = inst;
        //        index++;
        //    }
        //    else
        //    {
                fscanf(f, "%x", &inst);
                pc ++;
    
                if(inst >> 12 == 1)
                {
                    printAdd(inst);
                }
    
                if(inst >> 12 == 5)
                {
                    printAnd(inst);
                }
    
                if(inst >> 12 == 0)
                {
                    printBr(inst, pc);
                }
         }
    }

    The while loop is terminating when it reaches an instruction that starts with with a 'zero'. I am wondering if this could be the issue or if this is completely unrelated.This happens when it reaches the Branch (printBr). The opcode for branch on the LC3 begins with x0. The commented lines are other things I have tried (the variables used for these have been removed but I did have them when I attempted it.)

    I also attempted while(fgets(line, sizeof(line), f) != NULL) and I received the exact same result.

    Please help, I am stuck! Thank you in advance for your responses!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by haynesmk
    The while loop is terminating when it reaches an instruction that starts with with a 'zero'.
    The while loop keeps looping as long as fgets does not return a null pointer, which would happen when there is an input error or when end of file has been reached. The thing is, you are not actually making use of the array named line after fgets has read into it. My guess is that you want to use sscanf instead, e.g.,
    Code:
    void printAssembly(char fileName[])
    {
        int inst = 0;
        int pc = 0;
        FILE *f;
        char line[2048];
     
        f = fopen(fileName, "r");
        if (!f)
        {
            /* handle/report file open error */
            return;
        }
    
        if (fscanf(f, "%x", &pc) == 1)
        {
            while (fgets(line, sizeof(line), f))
            {
                if (sscanf(line, "%x", &inst) == 1)
                {
                    pc++;
    
                    if (inst >> 12 == 1)
                    {
                        printAdd(inst);
                    }
    
                    if (inst >> 12 == 5)
                    {
                        printAnd(inst);
                    }
    
                    if (inst >> 12 == 0)
                    {
                        printBr(inst, pc);
                    }
                }
                else
                {
                    /* handle/report file format error */
                }
            }
        }
        else
        {
            /* handle/report file format error */
        }
    
        fclose(f);
    }
    If the pc value is actually expected to be on a separate line, then you would do the same fgets + sscanf approach to extract it instead of calling fscanf.

    By the way, the fileName parameter should be a pointer to const char instead of a pointer to char. You could also move that part to a wrapper function that handles opening and closing the file, thereby changing this function's task scope to focus on reading from the file and printing accordingly (i.e., printAssembly's parameter would be a FILE* instead).
    Last edited by laserlight; 04-17-2015 at 02:25 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning A File Help
    By hopes death in forum C Programming
    Replies: 4
    Last Post: 12-01-2013, 10:44 PM
  2. OOP & File scanning.
    By epidemic in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2007, 02:46 PM
  3. file scanning?
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2005, 10:27 AM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM
  5. Text file scanning
    By spyderwb in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2002, 03:31 PM

Tags for this Thread