Thread: passing pointer to function

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

    passing pointer to function

    Hi all.

    Why does the while loop in the main() function keep executing?

    Thanks in advance.

    Code:
    // wordcount.c: count the number of occurrences of every word in a text file
    // 4 functions: openfile, countwords, sort, display
    
    #include <stdio.h>
    #include <string.h>
    
    #define FILENAMELENGTH 30
    #define SUCCESS 1
    #define FAILURE 0
    
    FILE *fp;
    
    int openfile(char *string);
    
    int main(void)
    {
        int result;
        char filename[FILENAMELENGTH];
    
        printf("Enter file to open: ");
        fgets(filename, FILENAMELENGTH, stdin);
        filename[strlen(filename) + 1] = '\0';
    
    
        while((result = openfile(filename)) == FAILURE)
        {
            printf("File could not be opened. Enter file to open: ");
            fgets(filename, FILENAMELENGTH, stdin);
            filename[strlen(filename) + 1] = '\0';
            result = openfile(filename);
        }
    
        return 0;
    }
    
    int openfile(char *string)
    {
        if((fopen(string, "r")) == NULL)
            return FAILURE;
        else
        {
            printf("\n%s opened successfully", string);
            return SUCCESS;
        }
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by cfanatic View Post
    Hi all.

    Why does the while loop in the main() function keep executing?

    Thanks in advance.

    Code:
    // wordcount.c: count the number of occurrences of every word in a text file
    // 4 functions: openfile, countwords, sort, display
    
    #include <stdio.h>
    #include <string.h>
    
    #define FILENAMELENGTH 30
    #define SUCCESS 1
    #define FAILURE 0
    
    FILE *fp;
    
    int openfile(char *string);
    
    int main(void)
    {
        int result;
        char filename[FILENAMELENGTH];
    
        printf("Enter file to open: ");
        fgets(filename, FILENAMELENGTH, stdin);
        filename[strlen(filename) + 1] = '\0';
    
    
        while((result = openfile(filename)) == FAILURE)
        {
            printf("File could not be opened. Enter file to open: ");
            fgets(filename, FILENAMELENGTH, stdin);
            filename[strlen(filename) + 1] = '\0';
            result = openfile(filename);
        }
    
        return 0;
    }
    
    int openfile(char *string)
    {
        if((fopen(string, "r")) == NULL)
            return FAILURE;
        else
        {
            printf("\n%s opened successfully", string);
            return SUCCESS;
        }
    }
    Because function openfile returns 0(FAILURE) again and again.Are you sure that the file exsists?Mind that in line 39 you use fopen with "r" so the file must exsists!

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Oh yes, forgot to assign the file to a pointer. Still stuck in the loop though.

    Code:
    // wordcount.c: count the number of occurrences of every word in a text file
    // 4 functions: openfile, countwords, sort, display
    
    #include <stdio.h>
    #include <string.h>
    
    #define FILENAMELENGTH 30
    #define SUCCESS 1
    #define FAILURE 0
    
    FILE *fp;
    
    int openfile(char *string);
    
    int main(void)
    {
        int result;
        char filename[FILENAMELENGTH];
    
        printf("Enter file to open: ");
        fgets(filename, FILENAMELENGTH, stdin);
        filename[strlen(filename) + 1] = '\0';
    
    
        while((result = openfile(filename)) == FAILURE)
        {
            printf("File could not be opened. Enter file to open: ");
            fgets(filename, FILENAMELENGTH, stdin);
            filename[strlen(filename) + 1] = '\0';
            result = openfile(filename);
        }
    
        return 0;
    }
    
    int openfile(char *string)
    {
        if((fp = fopen(string, "r")) == NULL)
            return FAILURE;
        else
        {
            printf("\n%s opened successfully", string);
            return SUCCESS;
        }
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  4. #4
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    .Are you sure that the file exsists
    Yep, it exists.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Part of the problem is in the lines "filename[strlen(filename) + 1] = '\0';" That is setting a character past the end of the string to zero.

    The other part of the problem is that fgets() leaves a '\n' in the input string (in quite a few circumstances) and few filenames contain a '\n' character.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by cfanatic View Post
    Yep, it exists.
    Are you sure that you input the name correctly?If the loop is keep executing then you have to input again and again.When you run the program you see the output of line 42?Logically no,because the fopen returns NULL every time you run openfile function..

    Tip: Use a do while for recursion instead of while or remove line 30,because it the procedure of opening the file fails then you call the openfile function twice at every loop

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by grumpy View Post
    Part of the problem is in the lines "filename[strlen(filename) + 1] = '\0';" That is setting a character past the end of the string to zero.

    The other part of the problem is that fgets() leaves a '\n' in the input string (in quite a few circumstances) and few filenames contain a '\n' character.
    cfanatic in order to check if this is problem(quite possible) try to print the data of char array filename and see if it holds the input correctly or not!

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    Wow, you gotta be fast on this forum!

    Code like this helped me understand fgets() and the '\n'.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
        char str[30];
        int i;
        printf ("Enter file name: ");
        fgets (str, sizeof (str), stdin);
        for (i = 0; i < strlen (str); i++)
        {
            printf ("%c\t%d\n", str[i], str[i]);
        }
        return 0;
    }
    Head over to the C String tutorial for ways of dealing with the '\n'.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If it's changed to filename[strlen(filename) - 1] = '\0' to get rid of the 'new line' then it works.
    The way the loop is structured you're opening the file twice.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by fnprintf View Post
    Wow, you gotta be fast on this forum!

    Code like this helped me understand fgets() and the '\n'.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
        char str[30];
        int i;
        printf ("Enter file name: ");
        fgets (str, sizeof (str), stdin);
        for (i = 0; i < strlen (str); i++)
        {
            printf ("%c\t%d\n", str[i], str[i]);
        }
        return 0;
    }
    Head over to the C String tutorial for ways of dealing with the '\n'.
    Try to write int main(void) instead of int main()
    And about the ASCII codes in line 12,there are not necessary ,the characters matter

  11. #11
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Thanks for the replies. This code works:

    Code:
    // wordcount.c: count the number of occurrences of every word in a text file
    // 4 functions: openfile, countwords, sort, display
    
    #include <stdio.h>
    #include <string.h>
    
    #define FILENAMELENGTH 30
    #define SUCCESS 1
    #define FAILURE 0
    
    FILE *fp;
    
    int openfile(char *string);
    
    int main(void)
    {
        int result;
        char filename[FILENAMELENGTH];
    
        printf("Enter file to open: ");
        fgets(filename, FILENAMELENGTH, stdin);
        filename[strlen(filename) - 1] = '\0';
    
        while((result = openfile(filename)) == FAILURE)
        {
            printf("File could not be opened. Enter file to open: ");
            fgets(filename, FILENAMELENGTH, stdin);
            filename[strlen(filename) - 1] = '\0';
        }
    
        return 0;
    }
    
    int openfile(char *string)
    {
        if((fp = fopen(string, "r")) == NULL)
            return FAILURE;
        else
        {
            printf("\n%s opened successfully", string);
            return SUCCESS;
        }
    }
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I notice that this declaration is at file scope:
    Code:
    FILE *fp;
    It should be local to the openfile function instead.

    For this:
    Code:
    int openfile(char *string);
    It would be better to make the parameter a const char* since the string will not be modified, and to use filename as the parameter name instead of string.

    Also, this has a small edge case error:
    Code:
    fgets(filename, FILENAMELENGTH, stdin);
    filename[strlen(filename) - 1] = '\0';
    Basically, fgets only stores the newline character if there is enough space for it. Hence, it would be better to write:
    Code:
    if (fgets(filename, FILENAMELENGTH, stdin))
    {
        char *p = strchr(filename, '\n');
        if (p)
        {
            *p = '\0';
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing a function pointer
    By Akkernight in forum C++ Programming
    Replies: 17
    Last Post: 03-22-2009, 04:41 AM
  2. c ++ passing a pointer into a function
    By Ron in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2008, 08:31 PM
  3. passing method pointer as a function pointer
    By sevcsik in forum C++ Programming
    Replies: 5
    Last Post: 12-30-2007, 06:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 06:40 AM