0

I'm doing an assignment for school using getop with ocelot, I have to count words and count the substrings in the document. I have the word counting working but I was not able to do the substring, I can only get to read/display what substring the user wants, that's what the program is about. Now, when I put for example on the commandline:

./hello -c -f substringToFind test.txt

it works but that's because the file test exists, if I say for example: ./hello -c -f hd test

it would give me a segmentation fault. How can I read if it was a s. fault and display a message? ex:

if(segmentation fault){
printf("...");
exit(1)
}

Thanks

Arturo
  • 103
  • 4

1 Answers1

1

This might be better answered in stackoverflow.com than here. I'm also guessing you meant getopt instead of getop.

If I'm getting your problem correctly, you might want to check for the file instead of the segmentation fault:

if( access( filename, F_OK ) != -1 ) {
    // file exists
} else {
    // file doesn't exist
}
Leo Gallego
  • 1,893
  • 9
  • 17