0xC0000005:访问冲突读取位置0x00000000。 c ++ vs2015

时间:2015-11-12 05:37:14

标签: c++ visual-studio-2015

我正在使用简单的文本解析器创建一个RPG,并且我继续遇到文本解析器中的access写入违规问题。到目前为止,这是我的代码:

/*
Author: Michael Norris
Program: Generiquest
Filename: TextParser.cpp
Maintainence Log:
10/28/2015 Created textParser.cpp


Notes:
This text parser is fairly inefficient, but is easier to manage and understand for beginners.

*/



#include <conio.h>
#include <stdio.h>
#include <Windows.h>
#include <time.h>
#include "myheader.h"
#include <iostream>
#include <string.h>


using namespace std;

class Words
{
public:
Words()
{
    word verbs[30];//This is an array of all the verbs
    strcpy(verbs[0].text, "Items"); 
    strcpy(verbs[1].text, "Stats");
    strcpy(verbs[2].text, "Use");
    strcpy(verbs[3].text, "Eat");
    strcpy(verbs[4].text, "Throw");
    strcpy(verbs[5].text, "Drop");
    strcpy(verbs[6].text, "Look");
    strcpy(verbs[7].text, "Move");
    strcpy(verbs[8].text, "Put");
    strcpy(verbs[9].text, "Speak");
    strcpy(verbs[10].text, "Attack");
    strcpy(verbs[11].text, "Go");
    strcpy(verbs[12].text, "Climb");
    strcpy(verbs[13].text, "Open");
    strcpy(verbs[14].text, "Take");
    strcpy(verbs[15].text, "Put");
    strcpy(verbs[16].text, "Kill");
    strcpy(verbs[17].text, "Get");
    strcpy(verbs[18].text, "LOL");

    //End of verb declarations
    for (int ele = 0; ele < 19; ele++)
    {
        verbs[ele].type = verb;
    }

}




};

Words mainWords;
void textParser()
{


    char str[51] = "Test String";
    char test[50] = "";
    char word1[20] = "";
    //char * word2;
    char word3[20] = "";
    char word4[20] = "";
    system("cls");
    scanf("%50[0-9a-zA-Z ]", &test);
    flushall();
    strcpy(word3, strtok(test, " ,.-"));
    int cray;
    for (bool correctI = false; correctI == false;)
    {

        if (word3 != NULL)
        {
            strcpy(word1, strtok(NULL, " ,.-"));
            cray = strcmp(word1, NULL);//Error thrown here
            if (cray != 0)
            {
                strcpy(word4, word1);

            }
        }
        printf("%s", word3);
        printf("%s", word4);
        cray = stricmp(word1, "Items");
        if (cray = 0)
        {
            printf("Success!!");

        }
        else
        {
            printf("Fail");
        }
    }
    _getch();


}

//TODO: use stricmp()

我在文本解析器函数中遇到了麻烦。

3 个答案:

答案 0 :(得分:1)

您正在使用类而不是C ++编写C.

C ++通过在它们之上构建精简抽象来避免许多指针问题。

例如,您可以使用char*,而不是使用std::string C风格的数组。所以不是不安全的

const char* word1 = "Whatever";
cray = stricmp(word1, "Items");
    if (cray == 0) {

你会得到

std::string word1 = "Whatever";
    if("Items" == word1) {

没有类似的

strcmp(word1, NULL)

因为将字符串与空指针进行比较是没有意义的(并且在C中不允许)。

您可能希望与空字符串进行比较:使用文字""

请注意,if (cray = 0)中也有错误(与上面的代码相比)。

此外,每当您遇到错误时,都不应立即在最近的论坛或StackOverflow上发布您的代码。相反,您需要在调试器下启动应用程序并尝试自己找出问题。这样您就可以更好地理解您的代码。

我认为在尝试编写更多C ++并用[C ++]标记更多问题之前,你应该选择一些关于该主题的好书。对于您和SO社区来说,这将更有成效。对此有一个很棒的帖子:

The Definitive C++ Book Guide and List

答案 1 :(得分:0)

void textParser()

{

char str[51] = "Test String";
char test[50] = "";
char word1[20] = "";
//char * word2;
char word3[20] = "";
char word4[20] = "";
system("cls");
system("cls");
scanf("%50[0-9a-zA-Z ]", &test);
flushall();
strcpy(word3, strtok(test, " ,.-"));
int cray;
for (bool correctI = false; correctI == false;)
{

    if (word3 != NULL)
    {
        strcpy(word1, strtok(NULL, " ,.-"));
        if (word1 != NULL)
        {
            strcpy(word4, word1);

        }
    }
    printf("%s", word3);
    printf("%s", word4);
    cray = stricmp(word1, "Items");
    if (cray = 0)
    {
        printf("Success!!");

    }
    else
    {
        printf("Fail");
    }
}
_getch();

}

仍会抛出一个accces违规错误        strcpy(word1,strtok(NULL,&#34;,.-&#34;));

答案 2 :(得分:0)

对您的(前)TL; DR kod列表

的一些评论
scanf("%50[0-9a-zA-Z ]", &test);
flushall();
strcpy(word3, strtok(test, " ,.-"));

评论:不要使用flushall()          见http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&id=1043284392          而只是有一个循环读取缓冲区,例如使用fgetc()          或使用替代方案 - 见下一条评论。

评论:要更多地控制输入,请使用fgets和          然后是sscanf,把它放在一个单独的函数中

for (bool correctI = false; correctI == false;)         

评论:这很不寻常,do { ... } while (!correctI)会更清晰。

strcpy(word1, strtok(NULL, " ,.-"));
cray = strcmp(word1, NULL);
if (cray != 0)
{
  strcpy(word4,word1);
}

备注:检查word1是否为NULL,而不是

char* tok = strtok(NULL, " ,.-");
if (tok != NULL)
{        
  strcpy(word4,tok);
}
word verbs[30];//This is an array of all the verbs
strcpy(verbs[0].text, "Items"); 
strcpy(verbs[1].text, "Stats"); 
...

评论:可以改为编写为

word verbs[] = { {"Items", 0}, {"Stats", 1}, ... };