【问题标题】:Access violation reading location 0x73726573访问冲突读取位置 0x73726573
【发布时间】:2015-10-23 19:08:57
【问题描述】:

我收到了这个奇怪的运行时错误,我不知道如何解决这个问题,我相信这对我有用,它是一个索引程序,它获取 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 字符 *

这就是我的命令行的样子,但这也带来了一个问题,我的权限被拒绝,即使它的权限等于 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

【问题讨论】:

  • 你用什么命令行来启动它?
  • 对不起编辑添加了~~刚刚添加了
  • 小想法:不要使用fgets(read, 100, file),而是使用fgets(read, 255, file),甚至更好的fgets(read, sizeof read, file)
  • 顺便说一句:代码很容易溢出word[]。在insert(word, Table, index) 之前推荐word[sizeof read] 和明确的word[k] = 0
  • @chux 一旦我可以运行它,我会试试这些谢谢!!

标签: c arrays string fopen deprecated


【解决方案1】:

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

你可以这样称呼它:

./concordance 15 not_used input.txt

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

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

【讨论】:

  • 另外,检查argc 以确保argv[whatever] 存在。经典的方法是从stdin 读取argc == 1argc[1] 中命名的文件,否则。
  • @dbush 这可能有效,但我现在不能说,因为当我尝试以这种方式传递它时,我得到的权限被拒绝
  • @lorehead 这是一种避免使用 fopen 的方法吗?
  • @FelixDaCat 如果是这种情况,则该文件不可读。你需要先解决这个问题。
  • @FelixDaCat 使用显示错误的命令行上的确切输入/输出更新您的问题。此外,在检查 file 是否为 NULL 的代码部分中使用 perror 而不是 printf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2016-05-28
  • 2017-01-09
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多