I looked up other people having this issue, but it seems that their problem was due to not having the correct size array or using an invalid pointer.
Here is the relevant code i'm using.
#include <stdio.h>
void Printcommands()
{
FILE * commandfile;
char str[256];
if ((commandfile = fopen("commands.txt", "r")) != 0) //Open the commands file in read mode
{
while (fgets(str, 256, commandfile) != 0) //While there are still more commands to be processed in the script file
{
printf("Content: %s\n", str); //Print line from file
}
fclose(commandfile); //Close command file
return; //return
}
else //Issue opening file
{
perror("Error opening commands.txt: "); //Print out error
return;
}
}
int main(int argc, char **argv)
{
Printcommands();
return 0;
}
Here is the contents of the commands.txt file
/bin/ls
asdf
asd
Here is my output
Content: /bin/ls
Content: asdf
Content: asd
Segmentation fault (core dumped)
Any idea what could be causing the segmentation fault? Thanks