Hamoudy
Hamoudy

Reputation: 589

Extract character from a string bug

I read in a temp variable from a file, this is one word, e.g. "and", however, when I extract the first character, e.g. temp[1], the program crashes when running, I have tried break points, and it is on this line.

This is what happens when I run the code: http://prntscr.com/2vzkmp

These are the words when I don't try to extract a letter: http://prntscr.com/2vzktn

This is the error when I use breakpoints: http://prntscr.com/2vzlr3

This is the line that is messing up: " printf("\n%s \n",temp[0]);"

Here is the code:

int main(void)
{
    char **dictmat;
    char temp[100];
    int i = 0, comp, file, found = 0, j = 0, foundmiss = 0;

    FILE* input;

    dictmat = ReadDict();


    /*opens the text file*/
    input = fopen("y:\\textfile.txt", "r");

    /*checks if we can open the file, otherwise output error message*/
    if (input == NULL)
    {
        printf("Could not open textfile.txt for reading \n");
    }
    else
    {
        /*allocates the memory location to the rows using a for loop*/

        do
        {
            /*temp_line is now the contents of the line in the file*/
            file = fscanf(input, "%s", temp);
            if (file != EOF)
            {

                lowercase_remove_punct(temp, temp);
                for (i = 0; i < 1000; i++)
                {
                    comp = strcmp(temp, dictmat[i]);
                    if (comp == 0)
                    {
                        /*it has found the word in the dictionary*/
                        found = 1;

                    }

                }

                /*it has not found a word in the dictionay, so the word must be misspelt*/
                if (found == 0 && (strcmp(temp, "") !=0))
                {

                    /*temp is the variable that is misspelt*/
                    printf("\n%s \n",temp[0]);

                    /*checks for a difference of one letter*/
                    //one_let(temp);
                }
                found = 0;
                foundmiss = 0;


            }

        } while (file != EOF);

        /*closes the file*/
        fclose(input);


    }


    free_matrix(dictmat);


    return 0;


}

Upvotes: 0

Views: 84

Answers (4)

Rahul
Rahul

Reputation: 3509

temp[0] is a charater. Thus if you are using

printf("\n%s \n",temp[0]);

it will print the string from address i.e. temp[0]. May be this location is not accessible, So it is crashing.

This change it to

printf("\n%c \n",temp[0]);

Upvotes: 1

user3213851
user3213851

Reputation: 1108

Why are you using %s as modifier, use %c

Upvotes: 0

SzG
SzG

Reputation: 12629

When printing a character, use %c, not %s. There is a fundamental difference between the two. The latter is for strings.

When printf encounters a %c it inserts one byte in ASCII format into the output stream from the variable specified.

When it sees a %s it will interpret the variable as a character pointer, and start copying bytes in ASCII format from the address specified in the variable, until it encounters a byte that contains zero.

Upvotes: 2

ziollek
ziollek

Reputation: 1993

print char - not string:

printf("\n%c \n",temp[0]);

Upvotes: 1

Related Questions