DISCLOSURE: This is homework.
The code below is meant to read a command file formatted like:
ADD 6 6 5
ADDTERM 0 1 1
MULTIPLY 2 40
and call the appropriate command using the given parameters. For some reason, while ADD and ADDTERM work as expected, I get a segfault when the multiply line is read.
int arg_1 = 0, arg_2 = 0, arg_3 = 0;
while(fscanf(commands, "%s", command) != EOF)
{
if(strcmp(command, "ADDTERM") == 0)
{
/*The following line runs fine!*/
fscanf(commands, "%d %d %d",
&arg_1, &arg_2, &arg_3);
printf("ADDTERM, Poly: %d, Coeff: %d, Exp: %d\n",
arg_1, arg_2, arg_3);
if(polys[arg_1] == NULL)
{polys[arg_1] = CreatePolynomial();}
AddTermToPoly(polys[arg_1], arg_2, arg_3);
}
else if(strcmp(command, "MULTIPLY") == 0)
{
/*The following line results in a segfault*/
fscanf(commands, "%d %d", &arg_1, &arg_2);
printf("MULTIPLY, Poly: %d, Multiplier: %d\n",
arg_1, arg_2);
MultiplyPoly(polys[arg_1], arg_2);
}
/*...*/
}
This is also only a problem on my school's Linux server. My home computer runs it just fine, using "gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)", whereas my school uses "gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)"
Any ideas why this would be? Your help is appreciated!
You need to leave room for a null terminus '\\0' in your string. You were okay with ADDTERM because it has 1 less letter than MULTIPLY.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.