0

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
whatsherface
  • 313
  • 2
  • 8
  • 21

2 Answers2

2

There are two likely causes of a segmentation fault in your code:

  • argv[1] does not exist, in which case attempting to access it may result in a segfault. Check if (argc > 1) to avoid this problem.
  • fopen does not successfully open the file, in which case attempting to read from the file (fgets) or fclose it will cause a segmentation fault. Immediately after a call to fopen, you should check that the return value is not NULL, e.g. if (dataFile == NULL).
alyu
  • 291
  • 2
  • 6
  • Thank you, I checked those things and in fact the return value was NULL. I copy-pasted the data from the file into a different file and resaved it just on a whim not expecting it to work but it did. I have no idea what was wrong with the original file since it too was just a .txt file. I'm still somewhat confused but glad it works now. – whatsherface Jul 18 '12 at 00:22
1

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.

int main(int argc, char** argv)
{
  if(argc > 1) // FIRST make sure that there are arguments, otherwise no point continuing.
  { 
    FILE* dataFile = NULL; // Initialize your pointers as NULL.
    const unsigned int SIZE = 180; // Try and use constants for buffers. 
                                      Use this variable again later, and if 
                                      something changes - it's only in one place.
    char string[SIZE];
    dataFile = fopen(argv[1], "r");
    if(dataFile) // Make sure your file opened for reading.
    {
      fgets(string, SIZE, dataFile); // Process your file.
      fclose(dataFile); // Close your file.
    }
  }
}

Remember that after this, string may still be NULL. See 'fgets' for more information.

Aesthete
  • 18,622
  • 6
  • 36
  • 45
  • Thank you for the suggestions. That is very helpful to me since I am trying to develop better coding practices. I have another question actually after reading through your post. I am now trying to read in each line of a multiline file until the end of file is reached and am having a lot of trouble. Could I use a loop and check if string is NULL in order to signal the end of the file? (Also, would it be better if I post this as a new question?) – whatsherface Jul 18 '12 at 04:29
  • Have look at `std::stringstream`, `std::getline' and their related classes. This should get you started - http://stackoverflow.com/questions/132358/how-to-read-file-content-into-istringstream – Aesthete Jul 18 '12 at 04:56