Crosswind

Untitled

Dec 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. int main() {
  2. errno = 0;
  3. const char *filename = "/Users/davidfrenzel/Google Drive/Studium/Semester 1/Programmierung 1/Projects/Uebung4/...";
  4.  
  5. FILE *file;
  6. file = fopen(filename, "r");
  7.  
  8. if (file == NULL) {
  9. fprintf(stderr, "%s\n", strerror(errno));
  10. exit(1);
  11. } else{
  12. printf("The file %s was Found\n", filename);
  13. }
  14. return 0;
  15. }
  16.  
  17. No such file or directory
  18. (You're right!)
  19.  
  20. BUT:
  21. int main() {
  22. errno = 0;
  23. const char *filename = "...";
  24.  
  25. FILE *file;
  26. file = fopen(filename, "r");
  27.  
  28. if (file == NULL) {
  29. fprintf(stderr, "%s\n", strerror(errno));
  30. exit(1);
  31. } else{
  32. printf("The file %s was Found\n", filename);
  33. }
  34. return 0;
  35. }
  36.  
  37. The file ... was Found
  38.  
  39. change the code to:
  40. int main() {
  41. errno = 0;
  42. const char *filename = "...";
  43.  
  44. FILE *file;
  45. file = fopen(filename, "r");
  46.  
  47. if (file == NULL || filename == "...") {
  48. fprintf(stderr, "%s\n", strerror(errno));
  49. exit(1);
  50. } else{
  51. printf("The file %s was Found\n", filename);
  52. }
  53. return 0;
  54. }
  55.  
  56. Of course errno won't be set to anything but that way I could tell the programm to return an error (in the function seen in question) if the filename == "...". Kind of as a workaround I guess
Advertisement
Add Comment
Please, Sign In to add comment