【问题标题】:Function keeps crashing without error message功能不断崩溃而没有错误消息
【发布时间】:2014-11-10 12:23:18
【问题描述】:

您好,我正在介绍 C 编程课程,因此我使用的是非常基本的代码。在这里,我只是想从主字符串中获取逗号矩阵。但是,当我尝试运行该程序时,它一直在我身上崩溃,我不知道我的问题是什么。我能够正确使用 fgets 功能,所以我认为它仍然可以正常工作。

CD Data.txt 文件

Eagles, Hotel California, 1976, Rock, 4
The Fratellis, Costello Music, 2006, Garage Rock, 5
Awolnation, Megalithic Symphony, 2011, Indie Rock, 5
Lindsey Stirling, Lindsey Stirling, 2012, Classical Crossover, 5
Arctic Monkeys, AM, 2013, Indie Rock, 4

计划

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define row 1000
#define column 1000

void getCommas(char str[], int commas[])
{
    int flag, count, index;

    count = 0;
    index = 0;
    flag = 1;
    while(flag = 1)
    {
        if(str[count] = ',')
        {
            commas[index] = count;
            index = index + 1;
        }
        count = count + 1;

        if(str[count] = '\0')
        {
            flag = 0;
        }
    }

}

int main()
{
    int i;

    char CdInfo[row][column];
    int Index[row][column];

    FILE *fp;
    fp = fopen("CD Data.txt","r");

    for(i=0; i<5; i++)
    {
        fgets(CdInfo[i], sizeof CdInfo, fp);
        //printf("%s\n",CdInfo[i]);
    }

    for (i=0; i<5; i++)
    {
        getCommas(CdInfo[i], Index[i]);
    }

    fclose(fp);
    return 0;
}

【问题讨论】:

  • 首先,将此if(str[count] = ',') 更改为if(str[count] == ','),将此if(str[count] = '\0') 更改为此if(str[count] == '\0')
  • 另外,将fgets(CdInfo[i], sizeof CdInfo, fp)改为fgets(CdInfo[i], sizeof CdInfo[i], fp)
  • 谢谢巴拉克!这有很大帮助,但是如果我将 CdInfo 更改为 CdInfo[i] 它并没有给我想要的东西,CdInfo 就像它一样工作正常。
  • 所有这一切都意味着你要么有一个非常糟糕的编译器(甚至比 1990 年的 Turbo C 还要糟糕,它会为此代码产生警告),或者更有可能的是,你禁用了基本的编译器警告。
  • 是否有重置按钮或默认设置?我几乎不知道如何使用 CodeBlocks,我还没有进入过设置。那里的语言对我来说是胡言乱语。

标签: c function crash


【解决方案1】:

这两个变量太大而不能入栈:

int main()
{
    int i;

    char CdInfo[row][column]; //<<
    int Index[row][column];   //<<

将它们声明为静态变量或全局变量。

还有:

while(flag = 1)

应该是

while(flag == 1)

和所有

if (str[count] = ...

应该是

if(str[count] == ...

【讨论】:

  • 非常感谢!我通常会更好地获得那些双 == 标志,也许现在还为时过早。而且我确实缩小了列,现在它可以工作了,不知道我理解为什么它以前没有工作。我以为你有足够的内存空间可以使用。我现在还有一个问题,逗号[0]=9,逗号[1]=13,逗号[2]=19,逗号[3]=31,它给我的不是我想要的,它在这里做什么?
  • @Maty 对不起,我没看懂你评论的最后一句话。也许您应该针对该特定问题发布一个新问题。
  • 我试过但必须等待 90 分钟。我从函数中打印了数组逗号 [],这些是我得到的结果。逗号[0]=9,逗号[1]=13,逗号[2]=19。逗号[3]=31.
  • @Maty:等 90 分钟?对于哪个输入,您会得到这个结果?我认为最好发布一个新问题。
  • 好的,我会在一个新问题中发布它。它说我每 90 分钟只能发帖一次。不过,感谢您的宝贵时间,我会等一下,并在等待时尝试弄清楚。
【解决方案2】:

你也应该考虑更换

while(flag = 1) {

与:

while(flag == 1)

【讨论】:

    【解决方案3】:
    // note:
    // the code is not making any use of more than one line of the
    // input file at any one time, so
    // only the current row actually needs to be defined
    
    // note:
    // this code makes no check for max length of each row (1000 char)
    // that could/will be a problem when the input line is longer than 1000 character
    
    // to avoid the error of writing an assignment rather than a literal,
    // place the literal on the left side, 
    // then the compiler will notify you of the error
    // rather than you having to spend time debugging the code
    // trying to find the error
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define row 1000
    #define column 1000
    
    //void getCommas(char str[], int commas[])
    void getCommas( char * pStr, int * pCommas )
    {
        //int flag, count, index;
    
        //count = 0;
        //index = 0;
        //flag = 1;
        int flag = 1;
        int count = 0;
        int index = 0;
    
        //while(flag = 1)
        // following while loop could eliminate 
        // the 'flag' variable and related code by using
        // and would be safer because never looking at string termination
        // character but once.
        // while( '\0' != pStr[count] )
        while( 1 == flag )
        {
            //if(str[count] = ',')
            if( ',' == pStr[count] )
            {
                pCommas[index] = count;
                index = index + 1;
            }
            count = count + 1;
    
            //if(str[count] = '\0')
            if( '\0' == pStr[count] )
            { // then found end of string
                flag = 0;
            }
        }
    }
    
    char CdInfo[row][column];
    int Index[row][column];
    
    int main()
    {
        int i = 0;
        int rowCount = 0;
    
        //char CdInfo[row][column]; // this is a huge item on the stack, 
        //int Index[row][column]; // this is a huge item on the stack, 
    
        //FILE *fp;
        FILE *fp = NULL;
        fp = fopen("CD Data.txt","r");
    
        // always check the result of calls to io functions
        if ( NULL == fp )
        { // then fopen failed
            perror( "fopen" );
            exit(1);
        }
    
        // implied else
    
        // there is no reasonable reason (in the real world) 
        // to expect the input to be only 5 lines
        //for(i=0; i<5; i++)
        //{
        //    fgets(CdInfo[i], sizeof CdInfo, fp);
        //    //printf("%s\n",CdInfo[i]);
        //}
        for( i=0; i<row; i++ )
        {
            // following line checks results of call to I/O function 
            if( 0 == fgets( CdInfo[i], row, fp ) ) { break; }
            // above line exits loop on end of file or I/O error
            rowCount++;
        }
    
        //for (i=0; i<5; i++)
        for( i = 0; i < rowCount; i++ )
        {
            getCommas(CdInfo[i], Index[i]);
        }
    
    
        fclose(fp);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多