Like Tree8Likes
  • 1 Post By std10093
  • 1 Post By grumpy
  • 1 Post By std10093
  • 1 Post By std10093
  • 1 Post By fnprintf
  • 1 Post By nonoob
  • 2 Post By laserlight

passing pointer to function

This is a discussion on passing pointer to function within the C Programming forums, part of the General Programming Boards category; Hi all. Why does the while loop in the main() function keep executing? Thanks in advance. Code: // wordcount.c: count ...

  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,681
    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!
    cfanatic likes this.

  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,306
    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 likes this.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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
    cfanatic likes this.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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!
    cfanatic likes this.

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    50
    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'.
    cfanatic likes this.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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.
    cfanatic likes this.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    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
    21,739
    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';
        }
    }
    AndiPersti and cfanatic like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:19 AM
  4. Replies: 4
    Last Post: 11-05-2006, 01:57 PM
  5. Passing Pointer to a function
    By Nishant Ghai in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 05:40 AM

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