简体   繁体   English

如何修复 C 中的分段错误错误

[英]How do I fix the segmentation fault error in C

I am new to C and I can't figure it out.我是 C 的新手,我想不通。 The code compiles and lets me run it one time but when I enter it gives me segmentation fault.代码编译并让我运行一次,但是当我输入它时,它给了我分段错误。 I ran gdb and backtraced it to find that the segmentation fault was in int main () in commissionOfAMechanic (name, &percent);我跑了 gdb 并回溯它发现分段错误在commissionOfAMechanic(name,&percent)中的int main()中; but I don't know how to make it work.但我不知道如何使它工作。 If someone could give me any types of suggestions to fix that error.如果有人可以给我任何类型的建议来解决该错误。 The files I used in the code are in the bottom.我在代码中使用的文件在底部。

#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;
}

在此处输入图像描述

In your code you ask from fscanf to read from your file, and then write the result to the address *comm, where comm is an integer and fscanf takes its value as a pointer (and of course that points to invalid memory).在您的代码中,您要求 fscanf 从您的文件中读取,然后将结果写入地址 *comm,其中 comm 是 integer 并且 fscanf 将其值作为指针(当然,它指向无效内存)。 Also in your temp buffer, you may have a buffer overflow when you read a string bigger than 16 bytes.同样在您的临时缓冲区中,当您读取大于 16 字节的字符串时,您可能会遇到缓冲区溢出。 Use fgets instead of fscanf to control your input.使用 fgets 而不是 fscanf 来控制您的输入。

Solution:解决方案:

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 );
}

Also in your void getWeeklyStatsOfaMechanic (char *name, int jobs[]) you have the same problem with fscanf同样在您的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);
    }

Also in your main:同样在你的主要:

scanf("%s", &name);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM