Reading a text file, and performing actions based on strings read

Posted by user1691766 on Stack Overflow See other posts from Stack Overflow or by user1691766
Published on 2012-09-23T03:32:21Z Indexed on 2012/09/23 3:37 UTC
Read the original article Hit count: 96

Filed under:

Let me start off by saying that I really am quite new to C. So basically I have a text file (contains around 30 assembly instructions, separated by new lines), and I am successfully reading them into my program. From here I need to perform certain actions based off each instruction. I would assume the best way to do this would be via a switch-case statement. However I am encountering a lot of problems due to me trying to compare strings that are 33 characters long. Can anyone please offer me advice on what I am doing wrong, or offer an alternative? Thanks in advance. Here is what I have so far:

char instruction[29][ 33 ];
int i,run;

i = 0;
run = 1;

FILE *instPtr;


/* LOADING INSTRUCTIONS FROM FILE INTO "instruction" ARRAY*/


if ( ( instPtr = fopen("MIPSinstructions.txt", "r") ) == NULL ) {
    printf("File could not be opened\n");
} // end if

else {
    fscanf( instPtr, "%s", instruction[0]);
    printf("%s\n", instruction[0]);
    while (!feof( instPtr ) ){
        i++;

        fscanf( instPtr, "%s", instruction[i]);
        printf("%s\n", instruction[i]);


    }
    fclose( instPtr );

    i = 0;          // Reset the counter

   } //end else

return 0;

}

© Stack Overflow or respective owner

Related posts about c