Thread: checking errors in the data of a text file

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    9

    Post checking errors in the data of a text file

    Hi I'm trying to make a program that opens a text file with assembly language reads the data and runs a error check on the data in the text file. For example say one line in the data has the op-code lda $600 when the correct op-code is meant to be ldaa $600 I want the program to be able to pick that error up.
    I have already got a half of the program that opens the text file.
    Code:
    #include <stdio.h>#include <string.h>
    
    
    int main(void) 
    {
    	FILE*fname;
    	char prompt;
    	char filename[15];
    	char text[100];
    
    
    	printf( "Please enter the name of the file you wish to open: \n" );
    
    
    	fgets(filename, sizeof(filename), stdin);
    	filename[strlen(filename)-1]='\0';
    
    
    	fname = fopen(filename, "r");
    	if (fname == NULL)
    	{
    		printf( "ERROR the file you have chosen cannot be opened \n" );
    		printf( "Please make sure the file exists \n");
    
    
    	}
    	while (fgets(text,100,fname) != NULL)
    		printf("\n%s", text);
    	scanf("\n%c",&prompt);
    }
    the assembly language is for a 68hc11 line assembler by the way
    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Did you have a question?

    Also, you should close the file when you're done, and return 0 at the end of "main()".

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    yea sorry if it wasn't to clear above. I'm wondering how I could write a code that reads the assembly language in the text file for any errors and then tells the user if there are. For example say one line in the data has the op-code lda $600 when the correct op-code is meant to be ldaa $600. How do I write a code that picks up this error?

    and thanks

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    There are different ways to go about this. One is to parse each string (line of assembly code) for the command, and compare that command to a list of legal commands.

    Do you have a sample of the input file you could post?

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    Yea here it is:
    name ex3
    org $500
    ldaa $600
    adda $601
    staa $602
    swi
    org $600
    fcb $20
    fcb $30
    end

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    So to refine the approach I suggested:

    - read a line and store it as a string
    - if it's a comment, label, or control, ignore it
    - otherwise, extract the command from the string
    - check the command against a list of known good commands
    - if it's an invalid command, flag an error

    Looking at your previous posts, this project might be more advanced than you're ready to handle, but if you take it one step at a time, you'll probably learn a lot in the process.

    If you haven't already read this link, I suggest you do; it's invaluable advice.
    A development process

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    Yea I'm kind of a beginner to C, but hopefully I'll work it out. Thanks a lot for your help.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You're welcome. If you have any more specific questions along the way, don't hesitate to ask. Best of luck!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is this homework or ?

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    Yea an assignment.... don't know how my lecture expected us to do this as most of my class are beginners in C programming

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So a simple clean up of your program would perhaps look like this.

    Code:
    #include <stdio.h>
    #include <string.h>
     
     
    int main(void)
    {
        FILE*fp;
        char prompt;
        char filename[15];
        char text[100];
        char assembly[10][50]= {
        {"name ex3"},
        {"org $500"},
        {"ldaa $600"},
        {"adda $601"},
        {"staa $602"},
        {"swi"},
        {"org $600"},
        {"fcb $20"},
        {"fcb $30"},
        {"end"}
        };
        printf( "Please enter the name of the file you wish to open: \n" );
     
     
        fgets(filename, sizeof(filename), stdin);
        filename[strlen(filename)-1]='\0';
     
     
        fp = fopen(filename, "r");
        if (fp == NULL)
        {
            printf( "ERROR the file you have chosen cannot be opened \n" );
            printf( "Please make sure the file exists \n");
     
     
        }
        //comparison loop 
        while (fgets(text,100,fp) != NULL) {
            printf("%s\n", text);
        }
       
       
       scanf("%c",&prompt);
    
    
       fclose(fp);
       return 0;
    
    }
    Aside from changing the file pointer's name, almost the only change is adding the assembly lines you posted - so it can be used to test the contents of the file. (also added: fclose(fp), return 0, and a pair of curly braces. Inside the format specifier for scanf("RIGHT HERE", &variableName), you should have no \n's. \n (newlines), go AT THE END of all printf() statements, (so the buffer gets flushed to the console or outprint stream).

    I'm assuming that the assembly lines you posted were correct, and copied that into the TestAssembly.txt file - but I copied it twice, and changed several lines so they are no longer correct.

    The above doesn't DO anything just yet, but it does give a compilable program, with the framework code you posted.

    The logic will be in that "comparison loop".

    See if that makes sense to you, and read up on strcmp() - part of string.h.

    Would a for loop from 0 to 9, using strcmp(string1, assembly[i]) be useful in this instance? Of course, the return from strstr() is an int, so you'd need to add that to your program - and one more int for the for() loop counter. (by tradition, called i).
    Last edited by Adak; 04-26-2013 at 07:10 PM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make that "the return from strcmp() is an int...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  2. Checking for repeated data: variable against file
    By eidgeare in forum C Programming
    Replies: 1
    Last Post: 12-25-2009, 05:01 PM
  3. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM
  4. Replies: 1
    Last Post: 10-30-2002, 05:45 AM
  5. Checking data types and errors
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 03-12-2002, 10:42 PM

Tags for this Thread