访问冲突读取位置0x73726573

时间:2015-10-23 19:08:58

标签: c arrays string fopen deprecated

我得到了这个奇怪的运行时错误,我不知道如何解决这个问题,我相信这对我有用,它一度是一个一致性程序,它接受一个txt文件中的每个单词并按顺序排除副本使用二叉搜索树。首先我不得不放入#define _CRT_SECURE_NO_DEPRECATE,因为fopen已经贬值了,它给我的alt(s_fopen)不起作用。下面发布的是我的Main和其他3个函数(一个用于在我的表中插入,一个用于打印我的表,另一个用于清空它)以及调试器告诉我的内容。另外我的主要是从unix命令行中获取表大小和txt文件。

#ifdef _WIN32
#define _CRT_SECURE_NO_DEPRECATE
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *Table[60] = { "" };

int insert(char *word, char *Table[], int n);
void empty(char *Table[], int n);
void print(char *Table[], int n);

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


    FILE *file;
    //position in the buffer
    int wordPos;
    //current line
    int line;
    //last character read
    char read[255];
    size_t len = 0;
    //open the file
    file = fopen(argv[3], "r");

    //die if can't open file
    if (!file) {
        printf("ERROR: Can't open file %s. \n", argv[1]);
        exit(1);
    }

    int i = 0;
    int j = 0;
    int index = 0;
    //read a char until end of file reached
    int end = atoi(argv[1]);
    while ((fgets(read, 100, file)) != NULL) {
        int k = 0;
        char word[10];
        for (j = 0; read[j] != '\0'; j++) {
            char x = read[j];
            if (read[j] != '\0'&& read[j] != ' ' && read[j] != ',' &&read[j] != '.'  && read[j] != '!'&&read[j] != '?' && read[j] != '\n' &&read[j] != ':' && read[j] != ';') {
                word[k] = tolower(read[j]);
                k++;
            }
            else {



                if (word[0] != '\0') {

                    index = insert(word, Table, index);
                }
                for (k = 0; k<10; k++) {
                    word[k] = '\0';
                }
                k = 0;



            }
        }

    }
    print(Table, end);
    empty(Table, index);
    return 0;

}

int insert(char *word, char *Table[], int n)
{
    int index = -1, k;

    int low = 0, high = n - 1, mid;

    if (word[0] == ' ')
        return n;
    while (low <= high)
    {
        mid = low + (high - low) / 2;
        if (strcmp(Table[mid], word) == 0)
        {
            index = mid;
            break;
        }
        else if (strcmp(Table[mid], word) < 0)
            low = mid + 1;
        else
            high = mid - 1;
    }
    if (index != -1 && index < n) return n;
    for (index = 0; index < n; index++)
    {
        if (strcmp(Table[index], word) < 0)
            continue;
        else
            break;
    }
    for (k = n - 1; k >= index; k--)
    {
        Table[k + 1] = Table[k];
    }
    Table[index] = _strdup(word);
    return n + 1;
}

void print(char *Table[], int n)
{
    int index;
    for (index = 0; index < n; index++)
    {
        printf("%d: %s\n", index + 1, Table[index]);
    }
}
void empty(char *Table[], int n)
{
    int index;


    for (index = 0; Table[index]; index++)
    {
        free(Table[index]);
        Table[index] = NULL;

    }
}

这是调试器所说的:

  

CunixProgAssign2ProgTest.exe中的0x0F85C311(ucrtbased.dll)抛出异常:0xC0000005:访问冲突读取位置0x73726573。

当我在调试器中查看argv [3]的值时,它会说这个

  
      
  • argv [3] 0x73726573 char *
  •   

这是我的命令行的样子,但这也是一个问题,即使它的权限等于777,我的权限也被拒绝;我认为这是因为我无法调试程序而没有错误。

shell:~> gcc -Wall -o concordance concordance.c
shell:~> ./concordance 15 < input.txt

它给我的错误看起来像这样

./:Permission denied

输入

shell:~> ./concordance 15 < input.txt

shell:~> ./concordance 15  input.txt

1 个答案:

答案 0 :(得分:2)

这:shell将<解释为输入重定向。它导致input.txt的内容被输入stdin。这也意味着您的计划的唯一参数是15。所以argv[3]没有指向任何东西,导致崩溃。

您可以这样称呼它:

./concordance 15 not_used input.txt

由于未使用argv[2],您可以在其中放置任意字符串。

要做的最好的事情是检查argc是否足够大,并且可能让程序从argv[2]读取文件名。

相关问题