我是C语言新手,我想不通。代码编译后让我运行一次,但当我输入时,却给我分段故障。我运行gdb,回溯发现分段故障是在 int main () 在 commissionOfAMechanic(name,&%)。 但我不知道如何让它工作。如果有人能给我任何类型的建议来解决这个错误。我在代码中使用的文件在底部。
#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从你的文件中读取,然后把结果写到地址*comm,其中comm是一个整数,fscanf把它的值作为一个指针(当然这指向的是无效的内存)。另外在你的临时缓冲区中,当你读取一个大于16字节的字符串时,你可能会出现缓冲区溢出。使用fgets代替fscanf来控制你的输入。
解决方法:在你的临时缓冲区中,当你读取大于16字节的字符串时,可能会出现缓冲区溢出。
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);