Like Tree1Likes
  • 1 Post By christop

parameters of fopen

This is a discussion on parameters of fopen within the C Programming forums, part of the General Programming Boards category; EDIT: I don't know why the code is all on a single line. Even after I edit the post to ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242

    parameters of fopen

    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;}
    Last edited by cfanatic; 07-07-2012 at 07:08 PM.

  2. #2
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    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 <stdlib.h>
    #include <stdio.h>
    
    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;
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    First of all, please fix your code formatting. Edit: you just fixed it right before my post.

    Second of all, I would chuck that book and find a better one if I were you.

    gets() is bad. There is no safe way to use it.

    Assigning the return value from getchar()/getc() to a char variable is (generally) bad. getchar() and getc() return an unsigned character converted to an int, so you can't reliably test for EOF if you save it to a char (which this code doesn't do properly either).

    At least it doesn't have "void main" or other travesties like that.

    To answer your question, though, mode is a string variable (technically it's an array of char).
    Last edited by christop; 07-07-2012 at 07:14 PM.
    cfanatic likes this.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Thanks christop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what does fopen do?
    By nullifyed in forum C Programming
    Replies: 2
    Last Post: 05-08-2010, 08:57 AM
  2. fopen
    By ericad in forum C Programming
    Replies: 3
    Last Post: 01-24-2010, 07:39 AM
  3. fopen
    By TeQno in forum C Programming
    Replies: 1
    Last Post: 05-22-2003, 11:24 AM
  4. need help on the parameters for fopen()
    By toaster in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 06:44 PM
  5. fopen help
    By plivermo in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21