【发布时间】: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