Ah, thanks! Makes sense. Just made command of size MAX_COMMAND_LENGTH + 1.
I am trying to to use fscanf to read the same file 2 different times in a loop but I am getting a seg fault. The idea is that the file will be formatted so that it has a command at the beginning of each line, followed by arguments for the command.
char* command; //Changed to char command[MAX_COMMAND_LENGTH + 1]
while(fscanf(file, "%s", command) == 1)
{
if (strcmp("CMD1", command) == 0)
{
int index, exp, coeff;
fscanf(file, "%d %d %d", &index, &exp, &coeff);
}
else if (strcmp("CMD2", command) == 0)
{
int num;
fscanf(file, "%d", &num);
}
}
You need to allocate memory for
command. For example:will allocate enough memory for 1024 characters.
Remember to
free(command)when you’re finished with it.