I’m receiving a segmentation fault when I try to print some variables in a struct to file. When I first wrote the code, I was able to print the result onto the screen just fine with the exception of a segmentation fault that shows up at the very end
However, when I changed my code so that I am printing to file, it creates the file, but the file is empty and a segmentation fault prints to screen instead.
Any advice?
/* all is an array of struct CASE,
artall is an array of struct ARTCOUNT,
pLast is last element in all */
void printArtCount (CASE* all, ARTCOUNT* artAll, CASE* pLast)
{
ARTCOUNT* artWalker = artAll;
CASE* walker;
char input[51];
FILE* spOut;
printf("File name for artist count output: ");
scanf("%50s", input);
spOut = fopen(input, "w");
while(artWalker->name)
{
fprintf(spOut, "%d ", artWalker->count);
fprintf(spOut, "%s\n", artWalker->name);
for(walker = all; walker <= pLast; walker++)
{
if (strcmp(walker->name, artWalker->name) == 0)
fputs(walker->art, spOut);
}
artWalker++;
}
return;
}
Edit: Fixed the code so that segmentation error no longer shows up.
EVerything works now.
Thanks.
You may be reading the array out of its limit.
Please try the following way of looping through the array elements.