The three things mentioned in the title are somewhat new to me. I am familiar with all of them conceptually but this is the first time I have tried to write my own program from scratch in C++ and it involves all three of these things. Here is the code:
int main(int argc, char *argv[])
{
FILE *dataFile;
char string [180];
dataFile = fopen(argv[1],"r");
fgets(string,180,dataFile);
fclose(dataFile);
}
It compiles fine, but when I execute using a simple input text file I get a segmentation fault. I have searched multiple tutorials and I can’t figure out why. Any help would be appreciated.
There are a few things you should be checking here. It still may not do what you expect, but it will avoid the errors you’re getting.
Remember that after this,
stringmay still be NULL. See ‘fgets’ for more information.