EDIT: I don't know why the code is all on a single line. Even after I edit the post to correct the format, the code still reverts to a single line. Blah.Hi folks.Below is sample code from a book that I am working through. Why does the program work when there is no "" around the mode?if ((fopen(filename, r) == NULL)) doesn't work, so why does the below program work?Thanks in advance.Code:/* Demonstrates the fopen() function. */#include #include int main( void ){ FILE *fp; char ch, filename[40], mode[4]; while (1) { /* Input filename and mode. */ printf("\nEnter a filename: "); gets(filename); printf("\nEnter a mode (max 3 characters): "); gets(mode); /* Try to open the file. */ if ( (fp = fopen( filename, mode )) != NULL ) { printf("\nSuccessful opening %s in mode %s.\n", filename, mode); fclose(fp); puts("Enter x to exit, any other to continue."); if ( (ch = getc(stdin)) == 'x') break; else continue; } else { fprintf(stderr, "\nError opening file %s in mode %s.\n", filename, mode); puts("Enter x to exit, any other to try again."); if ( (ch = getc(stdin)) == 'x') break; else continue; } } return 0;}



1Likes
LinkBack URL
About LinkBacks



