Exception thrown at 0x0FD61BCC (ucrtbased.dll) in cipherLab.exe: 0xC0000005: Access violation reading location 0x00000043

0

I am creating a program that will read a file into a string and will capitalize all characters and remove anything that is not a letter.

I am not sure what it is that I am doing wrong here. After the first execution of the else statement I receive an exception:

"Access violation reading location 0x00000043"

I have no idea what this means or how to even correct it.
Any help would be much appreciated.

Here's my code:

int main(void)
{
    char plainText[300], message;
    char cipherText[300];

    fpOutput = fopen("csis.txt", "w");

    processFile(plainText);
    cipher(cipherText, plainText);
    fclose(fpOutput);
    return 0;
}
//reads file into string
void processFile(char plainText[], int size)
{
    int textFileElement = 0;
    char textFile;

    if (!(fpCongress = fopen("congress.txt", "r")))
    {
        puts("File could not be opened for input");
        exit(1);
    }

    while (!feof(fpCongress))
    {
        for (textFileElement = 0; textFileElement < strlen(plainText); textFileElement++)
        {
            textFile = getc(fpCongress);
            plainText[textFileElement] = textFile;
            //checks for lowercase letters in string
            if (islower(plainText[textFileElement])) 
            {
                //capitalizes all lower case letters in strings
                putchar(toupper(plainText[textFileElement]));
            }
        }

        for (textFileElement = 0; textFileElement < strlen(plainText); ++textFileElement)
        {
            //removes any special characters and spaces
            if (!(plainText[textFileElement] >= 'A' && plainText[textFileElement] <= 'Z'
                    || plainText[textFileElement] == '\0'))
            {
                printf("%s\n", plainText[textFileElement]);
            }

            else
            {
                printf("%s\n", plainText[textFileElement]);
            }
        }

        printf("\n");
    }
    fclose(fpCongress);
}
c
visual-studio-2017
asked on Stack Overflow Aug 5, 2018 by Jonathan • edited Aug 5, 2018 by Jonathan

1 Answer

0

Thx for editing proper indentation.

Here is your issue:

for (textFileElement = '\0'; textFileElement < strlen(plainText); ++textFileElement)
{
    if (!(plainText[textFileElement] >= 'A' && plainText[textFileElement] <= 'Z'
            || plainText[textFileElement] == '\0'))
    {
        printf("%s\n", &plainText[textFileElement]);
    }

    else
    {
        printf("%s\n", plainText[textFileElement]);
    }
}

On the second print, you forgot to take the address, so the printf function tries to read a string at position plainText[textFileElement] and not at its position.

Here is corrected code:

for (textFileElement = '\0'; textFileElement < strlen(plainText); ++textFileElement)
{
    if (!(plainText[textFileElement] >= 'A' && plainText[textFileElement] <= 'Z'
            || plainText[textFileElement] == '\0'))
    {
        printf("%s\n", plainText + textFileElement);
    }

    else
    {
        printf("%s\n", plainText + textFileElement);
    }
}

Also took the chance to show you an alternate way to write this, expliciting that you are working with pointers.

Btw this resolve your segfault error, but might still not give the desired behavior, as your original code wasn't clear on that.

answered on Stack Overflow Aug 5, 2018 by Alceste_

User contributions licensed under CC BY-SA 3.0