【问题标题】:How do I fix the segmentation fault error in C如何修复 C 中的分段错误错误
【发布时间】:2020-06-14 14:36:53
【问题描述】:

我是 C 的新手,我无法弄清楚。代码编译并让我运行一次,但是当我输入它时,它给了我分段错误。我运行 gdb 并回溯它,发现分段错误在 commissionOfAMechanic (name, &percent); 中的 int main () 但我不知道如何做到这一点工作。如果有人可以给我任何类型的建议来解决该错误。我在代码中使用的文件在底部。

#include <stdio.h> 
#include <string.h>
#include <assert.h>
#include <stdlib.h>

void commissionOfAMechanic (char *mechanicName, int *comm)
{
    FILE * fp = fopen ("NameCommissions.txt", "r");
    if ( fp == NULL )
    {
        printf ( "Unable to open the file\n" );
        exit (-1);
    }

    char temp [16];
    int commission;

    int i;
    for ( i = 0 ; i < 4 ; i++ )
    {
        fscanf ( fp, "%s %d", temp, *comm);
        if (temp, *mechanicName)
            break;
    }

    fclose (fp);
    printf ( "The name given is %s and his commission is %d \n", temp, *comm );
}

void priceOfAllServices ( float pr[])
{
    FILE * fp = fopen ( "PriceOfServices.txt", "r");
    if (!fp)
    {
        printf ( "Can't open file.\n");
        exit (-1);
    }

    char temp [16];
    fscanf (fp, "%s %d", temp, &pr[0]);
    fscanf (fp, "%s %d", temp, &pr[1]);
    fscanf (fp, "%s %d", temp, &pr[2]);
    fscanf (fp, "%s %d", temp, &pr[3]);

    fclose (fp);
}

void getWeeklyStatsOfaMechanic (char *name, int jobs[])
{
    FILE * fp = fopen ("JobsDone.txt", "r");
    if (! fp)
    {
        printf ("Can't open file.\n");
        exit (-1);
    }

    char temp [16];
    fscanf (fp, "%s %d", temp, &jobs[0]);
    fscanf (fp, "%s %d", temp, &jobs[1]);
    fscanf (fp, "%s %d", temp, &jobs[2]);
    fscanf (fp, "%s %d", temp, &jobs[3]);



    int i;
    for ( i = 0 ; i < 4 ; i++ )
    {
        fscanf (fp, "%s%d%d%d%d", temp, jobs[0], jobs[1], jobs[2], jobs[3]);
        if (strcmp (temp, name ))
        {
            break;
        }
    }
    printf ( "Jobs done by %s %d %d %d %d \n", temp, jobs[0], jobs[1], jobs[2], jobs[3]);
    fclose (fp);
}

int main ()
{
    int percent;
    char name[16];
    int jobs[4];
    float prices[4];

    printf ( "Please enter the name of the Mechanic\n");
    scanf ("%s", name);

    commissionOfAMechanic (name, &percent);

    priceOfAllServices (prices);

    getWeeklyStatsOfaMechanic (name, jobs);

    float total = jobs[0]*prices[0] +
                  jobs[1]*prices[1] +
                  jobs[2]*prices[2] +
                  jobs[3]*prices[3];

    printf ( "Name = %s Total Commission = %.2f\n", name, total);

    return 0;
}

【问题讨论】:

  • 其实应该是fscanf ( fp, "%s %d", temp, comm);,因为comm已经是一个int指针了。
  • 除此之外,您可能不想要if (temp, *mechanicName),因为它与if (*mechanicName) 相同,我想您错过了使用strcmp
  • 警告你可以做printf ( "The name given is %s and his commission is %d \n", temp, *comm );即使你没有找到,因为i达到4

标签: c linux gcc gcc-warning


【解决方案1】:

在您的代码中,您要求 fscanf 从文件中读取,然后将结果写入地址 *comm,其中 comm 是一个整数,fscanf 将其值作为指针(当然,它指向无效内存) . 同样在您的临时缓冲区中,当您读取大于 16 字节的字符串时,您可能会遇到缓冲区溢出。使用 fgets 而不是 fscanf 来控制您的输入。

解决方案:

void commissionOfAMechanic (char *mechanicName, int *comm)
{
     FILE * fp = fopen ("NameCommissions.txt", "r");

     if ( fp == NULL )
     {
         printf ( "Unable to open the file\n" );
         exit (-1);
     }

     char temp [16];
     int commission;

     int i;
     for ( i = 0 ; i < 4 ; i++ )
     {
         fscanf ( fp, "%s %d", &temp, comm);

         if (temp, *mechanicName) // i am not sure what you are doing here, maybe you mean if(strcmp(temp, mechanicName) == 0) ?
             break;
     }

     fclose (fp);
     printf ( "The name given is %s and his commission is %d \n", temp, *comm );
}

也在你的void getWeeklyStatsOfaMechanic (char *name, int jobs[]) 你对 fscanf 有同样的问题

void getWeeklyStatsOfaMechanic (char *name, int jobs[])
{
       FILE * fp = fopen ("JobsDone.txt", "r");

       if (! fp)
       {
          printf ("Can't open file.\n");
          exit (-1);
       }

       char temp [16];
       fscanf (fp, "%s %d", &temp, &jobs[0]);
       fscanf (fp, "%s %d", &temp, &jobs[1]);
       fscanf (fp, "%s %d", &temp, &jobs[2]);
       fscanf (fp, "%s %d", &temp, &jobs[3]);


       int i;

       for ( i = 0 ; i < 4 ; i++ )
       {
           fscanf (fp, "%s%d%d%d%d", &temp, &jobs[0], &jobs[1], &jobs[2], &jobs[3]);

           if (strcmp (temp, name ))
           {
                break;
           }
       }
       printf ( "Jobs done by %s %d %d %d %d \n", temp, jobs[0], jobs[1], jobs[2], jobs[3]);
       fclose (fp);
    }

也在你的主目录中:

scanf("%s", &name);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多