C中读取位置0x00000000的访问冲突

C中读取位置0x00000000的访问冲突,c,access-violation,c-string,C,Access Violation,C String,我越来越 访问冲突读取位置0x00000000 当我试着运行我的程序时。我可以构建一个完全没有错误的程序,但是当我试图运行它时,它崩溃了。我在StackOverflow这里读到,当指针指向任何东西时,就会出现这个问题。我已经搜索了代码,但没有成功。有人知道问题出在哪里吗 main.c #include <stdio.h> #include <stdlib.h> #include "functions.h" #define FILENAME 256 /* * */

我越来越

访问冲突读取位置0x00000000

当我试着运行我的程序时。我可以构建一个完全没有错误的程序,但是当我试图运行它时,它崩溃了。我在StackOverflow这里读到,当指针指向任何东西时,就会出现这个问题。我已经搜索了代码,但没有成功。有人知道问题出在哪里吗

main.c

#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
#define FILENAME 256


/*
* 
*/



int main(int argc, char** argv) {


char * data = readFromFile("input.txt");
int arrLength = getArrayLength(data);
char * encryptedData = (char *)malloc(arrLength * sizeof(char) + 1 * sizeof(char));
decrypt(data, arrLength, 1, encryptedData);
encrypt(data, arrLength, 1, encryptedData);
int menu = 0;
int key;

printf("Enter 1 for decrypt or 2 for encrypt from input.txt");

scanf("%d", &menu);
if (menu == 1) {
    printf("Enter key:");
    scanf("%d", &key);
    encrypt(data, arrLength, key, encryptedData);

}
else {
    printf("Enter key:");
    scanf("%d", &key);
    decrypt(data, arrLength, key, encryptedData);
}

writeToFile("output.txt", encryptedData);

printf("\n Enter any key to exit 11: ");
getchar();
return (1);
}
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
char * readFromFile(char * fileName) {
FILE *fp;
char c;
if ((fp = fopen(fileName, "r")) == NULL) {
    puts("Error: input file invalid");
    return NULL;
}

int count = 0;
while ((c = fgetc(fp) != EOF)) count++;
rewind(fp);
char *data = (char *)malloc(count * sizeof(char) + 1 * sizeof(char));
count = 0;
while (!feof(fp)) {
    fscanf(fp, "%c", &data[count++]);
}

fclose(fp);
return data;
    }

    void writeToFile(char * fileName, char *data) {
FILE *fp;
if ((fp = fopen(fileName, "w")) == NULL) {
    puts("Error: output file invalid");
}
else {
    int i = 0;
    while (data[i] != '\0') {
        fputc(data[i], fp);
        i++;
    }
    fclose(fp);
}
    } 

    int encrypt(char *plainText, int arrLength, int key, char *cipherText) {
if (key > 25 || key < 1) {
    puts("Error: invalid key");
    return 0;
}
for (int i = 0; i < arrLength; i++) {
    if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90)) {
        // only encrypt alpha characters
        cipherText[i] = plainText[i] + key;
    }
    else {
        cipherText[i] = plainText[i];
    }
}
return 1;
    }
    int decrypt(char *plainText, int arrLength, int key, char *cipherText) {
if (key > 25 || key < 1) {
    puts("Error: invalid key");
    return 0;
}
for (int i = 0; i < arrLength; i++) {
    if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90)) {
        // only decrypt alpha characters
        cipherText[i] = plainText[i] - key;
    }
    else {
        cipherText[i] = plainText[i];
    }
}
return 1;
    }
    int getArrayLength(char *data) {
int i = 0;
while (data[i] != '\0') {
    i++;
}
return i;
}

实际错误意味着
NULL
指针取消引用,这可能是因为您从未检查
malloc()
的返回值,您必须检查的另一件事是
scanf()
的返回值

此外,您永远不会终止从文件读取的数据,也不会终止您加密的数据。然而,当您尝试这样的代码时,您将其视为

while (data[i] != '\0')
    i++;
我假设您不允许使用
strlen()
,因为这正是
strlen()
所做的

此外,您不需要遍历文件中的每个字节来计算其长度。试试这个

#include <stdio.h>
#include <stdlib.h>

#define FILENAME 256

char *
readFromFile(char * fileName)
{
    FILE *fp;
    size_t length;
    char *data;

    if ((fp = fopen(fileName, "r")) == NULL)
    {
        puts("Error: input file invalid");
        return NULL;
    }

    fseek(fp, 0L, SEEK_END);
    length = ftell(fp);
    rewind(fp);

    data = malloc(length + 1);
    if (data == NULL)
    {
        puts("Error: allocating memory");
        fclose(fp);
        return NULL;
    }

    if (fread(data, 1, length, fp) < length)
    {
        puts("Error: reading from the file");

        free(data);
        fclose(fp);
        return NULL;
    }
    // `nul' terminate please
    data[length] = '\0';
    puts(data);
    fclose(fp);

    return data;
}

void
writeToFile(char * fileName, char *data)
{
    FILE *fp;
    if ((fp = fopen(fileName, "w")) == NULL)
        puts("Error: output file invalid");
    else
    {
        for (int i = 0 ; data[i] != '\0' ; ++i)
            fputc(data[i], fp);
        fclose(fp);
    }
}

int
encrypt(char *plainText, size_t arrLength, int key, char *cipherText)
{
    if (key < 25 && key >= 1)
    {
        for (size_t i = 0 ; i < arrLength ; i++)
        {
            if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90))
            {
                // only encrypt alpha characters
                cipherText[i] = plainText[i] + key;
            }
            else
            {
                cipherText[i] = plainText[i];
            }
        }
        // `nul' terminate please
        cipherText[arrLength] = '\0';
        return 1;
    }
    puts("Error: invalid key");
    return 0;
}
int
decrypt(char *plainText, size_t arrLength, int key, char *cipherText)
{
    if (key < 25 && key >= 1)
    {
        for (size_t i = 0; i < arrLength ; i++)
        {
            if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90))
            {
                // only decrypt alpha characters
                cipherText[i] = plainText[i] - key;
            }
            else
            {
                cipherText[i] = plainText[i];
            }
        }
        // `nul' terminate please
        cipherText[arrLength] = '\0';
        return 1;
    }
    puts("Error: invalid key");
    return 0;
}

int
getArrayLength(char *data)
{
    size_t length;
    for (length = 0 ; data[length] != '\0' ; ++length);
    return length;
}

int
main(int argc, char** argv)
{
    char * data;
    int arrLength;
    char *encryptedData;
    int menu = 0;
    int key;

    data = readFromFile("input.txt");
    if (data == NULL)
        return -1;
    arrLength = getArrayLength(data);
    encryptedData = malloc(arrLength + 1);
    if (encryptedData == NULL)
        return -1;
    decrypt(data, arrLength, 1, encryptedData);
    encrypt(data, arrLength, 1, encryptedData);

    printf("Enter 1 for decrypt or 2 for encrypt from input.txt ");

    if (scanf("%d", &menu) != 1)
        return -1;
    if (menu == 1)
    {
        printf("Enter key: ");
        if (scanf("%d", &key) != 1)
            return -1;
        encrypt(data, arrLength, key, encryptedData);
    }
    else
    {
        printf("Enter key: ");
        if (scanf("%d", &key) != 1!)
            return -1;
        decrypt(data, arrLength, key, encryptedData);
    }
    writeToFile("output.txt", encryptedData);

    printf("\n Enter any key to exit 11: ");

    free(data);
    free(encryptedData);
    return (1);
}
#包括
#包括
#定义文件名256
煤焦*
readFromFile(字符*文件名)
{
文件*fp;
尺寸与长度;
字符*数据;
if((fp=fopen(文件名,“r”))==NULL)
{
puts(“错误:输入文件无效”);
返回NULL;
}
fseek(fp,0L,SEEK_END);
长度=ftell(fp);
倒带(fp);
数据=malloc(长度+1);
如果(数据==NULL)
{
puts(“错误:分配内存”);
fclose(fp);
返回NULL;
}
if(fread(数据,1,长度,fp)=1)
{
对于(大小i=0;i如果((明文[i]>=97 | |明文[i]=65 | | |明文[i]您的程序很长。您可以尝试缩小它的范围吗?欢迎使用堆栈溢出!。欢迎使用堆栈溢出!请参阅欢迎使用堆栈溢出!请先阅读第页。可能重复的
#include <stdio.h>
#include <stdlib.h>

#define FILENAME 256

char *
readFromFile(char * fileName)
{
    FILE *fp;
    size_t length;
    char *data;

    if ((fp = fopen(fileName, "r")) == NULL)
    {
        puts("Error: input file invalid");
        return NULL;
    }

    fseek(fp, 0L, SEEK_END);
    length = ftell(fp);
    rewind(fp);

    data = malloc(length + 1);
    if (data == NULL)
    {
        puts("Error: allocating memory");
        fclose(fp);
        return NULL;
    }

    if (fread(data, 1, length, fp) < length)
    {
        puts("Error: reading from the file");

        free(data);
        fclose(fp);
        return NULL;
    }
    // `nul' terminate please
    data[length] = '\0';
    puts(data);
    fclose(fp);

    return data;
}

void
writeToFile(char * fileName, char *data)
{
    FILE *fp;
    if ((fp = fopen(fileName, "w")) == NULL)
        puts("Error: output file invalid");
    else
    {
        for (int i = 0 ; data[i] != '\0' ; ++i)
            fputc(data[i], fp);
        fclose(fp);
    }
}

int
encrypt(char *plainText, size_t arrLength, int key, char *cipherText)
{
    if (key < 25 && key >= 1)
    {
        for (size_t i = 0 ; i < arrLength ; i++)
        {
            if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90))
            {
                // only encrypt alpha characters
                cipherText[i] = plainText[i] + key;
            }
            else
            {
                cipherText[i] = plainText[i];
            }
        }
        // `nul' terminate please
        cipherText[arrLength] = '\0';
        return 1;
    }
    puts("Error: invalid key");
    return 0;
}
int
decrypt(char *plainText, size_t arrLength, int key, char *cipherText)
{
    if (key < 25 && key >= 1)
    {
        for (size_t i = 0; i < arrLength ; i++)
        {
            if ((plainText[i] >= 97 || plainText[i] <= 122) || (plainText[i] >= 65 || plainText[i] <= 90))
            {
                // only decrypt alpha characters
                cipherText[i] = plainText[i] - key;
            }
            else
            {
                cipherText[i] = plainText[i];
            }
        }
        // `nul' terminate please
        cipherText[arrLength] = '\0';
        return 1;
    }
    puts("Error: invalid key");
    return 0;
}

int
getArrayLength(char *data)
{
    size_t length;
    for (length = 0 ; data[length] != '\0' ; ++length);
    return length;
}

int
main(int argc, char** argv)
{
    char * data;
    int arrLength;
    char *encryptedData;
    int menu = 0;
    int key;

    data = readFromFile("input.txt");
    if (data == NULL)
        return -1;
    arrLength = getArrayLength(data);
    encryptedData = malloc(arrLength + 1);
    if (encryptedData == NULL)
        return -1;
    decrypt(data, arrLength, 1, encryptedData);
    encrypt(data, arrLength, 1, encryptedData);

    printf("Enter 1 for decrypt or 2 for encrypt from input.txt ");

    if (scanf("%d", &menu) != 1)
        return -1;
    if (menu == 1)
    {
        printf("Enter key: ");
        if (scanf("%d", &key) != 1)
            return -1;
        encrypt(data, arrLength, key, encryptedData);
    }
    else
    {
        printf("Enter key: ");
        if (scanf("%d", &key) != 1!)
            return -1;
        decrypt(data, arrLength, key, encryptedData);
    }
    writeToFile("output.txt", encryptedData);

    printf("\n Enter any key to exit 11: ");

    free(data);
    free(encryptedData);
    return (1);
}