Problems with file pointer in functions

This is a discussion on Problems with file pointer in functions within the C Programming forums, part of the General Programming Boards category; please help urgently I cannot get the functions to work. the code within the functions works well without the the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Problems with file pointer in functions

    please help urgently

    I cannot get the functions to work. the code within the functions works well without the
    the functions. Am I passing the pointer correctly.
    Basically what I wanted to do, is make the text file accessible from within each function get perform a task.

    Code:
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    #include<ctype.h>
    
    void Keyword(FILE*fptr,int);			/* Keyword function Prototype*/
    void Long_Word(FILE*,char);		/* Longest Word function Prototype*/
    void N_sentence(FILE*,int);		/* Sentence function Prototype*/
    void N_Quotations(FILE*,int);	/* Double Quotes function Prototype*/
    void N_Vowels(FILE*,int);		/* Vowel function Prototype*/
    void N_Zeros(FILE*,int);			/* Zero function Prototype*/
    
    
    int main () /*MAIN PROGRAM*/
    { 
    
    char keyword [25];    /*holds the keyword to be searched*/
    char kw [25];
    int lw [25];
    int ns=0;
    int nq=0;
    int nv=0;
    int nz=0;
    
    
    char filename [30]; /*stores the filename of the file to be open*/
    
    printf("Enter Filename:  ");
    scanf("%s",filename);
    FILE *fptr;                   /*file pointer to fptr*/
    fptr = fopen(filename,"r");   /*opens file and reads from it*/
    
    if (fptr == NULL)            /*if file is empty do not open*/
    {
    	printf("File Cannot Be Open.\n");
    	printf("Please Try Again\a\a");
    	printf("\n\n\n");
    	system ("cls");
    	system("pause");
    	printf("Enter Filename:");
    	scanf("%s",filename);
    	
    }
    
    	 kw=Keyword(*fptr);			/* Function Call*/
    	 lw=Long_Word();		/* Function Call*/
    	 ns=N_sentence();		/* Function Call*/
    	 nq=N_Quotations();	/* Function Call*/
    	 nv=N_Vowels();		/* Function Call*/
    	 nz=N_Zeros();			/*Function Call*/
    
    
    fclose(fptr); /*CLOSE FILE POINTER*/
    return 0;
    }
    
    
    /*****************************************************************************************/
    /*Search Keyword Function*/
    
    
    void Keyword(FILE*fptr,int k_count)
    {
    	
    	char key_temp [25];   /*holds each string in the text file to be compared with keyword*/
    	int k_word=0;         /*k_word is used as a counter and is initialized to zero*/
    	int i;                /*i checks each character*/
    	char keyword [25];    /*keyword enter by user*/
    	
    	/*fptr file pointer*/
    	fptr=fopen("lamming.txt","r");  /*opens file and reads from it*/
    	
    	printf("Enter A Single keyword:");
    	scanf("%s",keyword);
    	
    	fscanf(fptr,"%s",key_temp);	
    	
    	while(!feof(fptr))
    	{
    		
    		for(i=0; i<=24; i++)
    			{
    				key_temp[i]=tolower(key_temp[i]);
    			}
    		
    		if(strcmp(keyword,key_temp)==0)
    			{	
    				k_word++;
    			}
    		
    		fscanf(fptr,"%s",key_temp);
    	}
    
    	printf("The Keyword %s Occurs %d time(s).",keyword,k_word);
    fclose(fptr);
    }	
    
    /*****************************************************************************************/
    /*The longest word function*/
    
    void Long_Word(FILE *Fptr,char long_word[])
    {
    	
    	
    	char longest_word [25]; /*longest word */
    	char lword [25];
    	int length=0;
    	int lw_counter=0;
    
    
    	
    	fptr=fopen("lammin.txt","r");
    	fscanf(fptr,"%s",longest_word);
    	length=strlen(longest_word);
    	strcpy(lword,longest_word);
    
    while(!feof(fptr))
    	{
    		 if (strlen(longest_word)>strlen(lword))
    			{
    				length=strlen(lword);
    				strcpy(lword,longest_word);
    			}
    		
    		fscanf(fptr,"%s",longest_word);
    
    	}
    printf("The Longest Word In The File Is ""%s"" " ,lword);
    fclose(fptr);
    }
    
    /********************************************************************************/
    
    /*Function that counts the number of sentences within the file*/
    void N_sentence(FILE *fptr,int nsent)
    {
    
    char fullstop ='.';
    char numb_sentences;
    int s_count=0;
    
    
    
    fptr=fopen("lammin.txt","r");
    
    while(!feof(fptr))
    	{
    		fscanf(fptr,"%c",&numb_sentences);
    		if (numb_sentences == fullstop)
    			{	
    				
    				s_count++;
    			}		
    	}
    printf("There are %d sentences in this file.",s_count);
    fclose(fptr);
    }
    /******************************************************************************/
    /*Function counts the number of double quotation marks*/
    
    void N_Quotations(FILE *fptr,int quotes)
    {
    	
    	
    	fptr=fopen("lammin.txt","r");
    	
    	char numb_quotes;    /*numb_quotes searches the file for quotes*/ 
    	char n_quotes ='"';  /*n_quotes is a constant*/ 
    	int quotes=0;        /*quotes reps. the counter*/
    
    
    while(!feof(fptr))
    	{
    		fscanf(fptr,"%c",&numb_quotes);
    			if(numb_quotes == n_quotes)
    				{
    					quotes++;
    					printf("%c",numb_quotes);
    				}
    	}
    printf("The Estimated Number Of Double Quotation Marks Are %d \n",quotes);
    fclose(fptr);
    }
    
    /*********************************************************************************/
    /*The function counts the frequencies of all the vowels found in the file*/
    
    void N_Vowels(FILE *fptr,int vowels)
    {
    	
    	
    	fptr=fopen("lammin.txt","r");
    
    	char vowels; /*vowels checks each character in the file*/
    	int v_a=0; /*counter for vowel 'a'*/
    	int v_e=0; /*counter for vowel 'e'*/
    	int v_i=0; /*counter for vowel ''*/
    	int v_o=0; /*counter for vowel 'o'*/
    	int v_u=0; /*counter for vowel 'u'*/
    
    
    
    
    	while(!feof(fptr))
    	{
    		fscanf(fptr,"%c",&vowels);
    						
    		
    		if(vowels == 'a'|| vowels == 'A') 
    				v_a++;
    			
    				if(vowels == 'e'|| vowels =='E')
    						
    						v_e++;
    					
    						if(vowels == 'i'|| vowels =='I')
    							
    								v_i++;
    							
    								if(vowels == 'o'|| vowels == 'O')
    									
    										v_o++;
    									
    										if(vowels == 'u'|| vowels == 'U')
    											
    												v_u++;
    											
    	}
    
    printf("The vowel A occurs %d times in the file.\n",v_a);
    printf("The vowel E occurs %d times in the file.\n",v_e);
    printf("The vowel I occurs %d times in the file.\n",v_i);
    printf("The vowel O occurs %d times in the file.\n",v_o);
    printf("The vowel U occurs %d times in the file.\n",v_u);
    fclose(fptr);
    }
    
    /********************************************************************************/
    /*Function Searches the file for the number of zeros and prints the amount found*/
    
    void N_Zeros(FILE *fptr,int zeros)
    {
    
    	char zeros; /*zero checks each character for zero n the file*/
    	int z=0;
    
    	
    	fptr=fopen("lammin.txt","r");
    
    	while(!feof(fptr))
    		{
    			fscanf(fptr,"%c",&zeros);
    				if (zeros == '0')
    				{
    					z++;
    				}
    
    		}
    printf("The Number Zero Occurs In The File %d Times.\n",z);
    fclose(fptr);
    }
    /**************************************************************************************/

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should be using Keyword(fptr), not Keyword(*fptr).

    What is it that doesn't work?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    please help urgently
    And you think this will make the difference between someone who knows the answer answering, and not answering?
    I cannot get the functions to work. the code within the functions works well without the
    the functions. Am I passing the pointer correctly.
    Basically what I wanted to do, is make the text file accessible from within each function get perform a task.
    Short answer: No.

    Code:
    	 kw=Keyword(*fptr);			/* Function Call*/
    passes the CONTENT of the structure behind a FILE *. You want to actually pass the pointer itself.

    If we take this code as an example (they are all pretty much the same, but more or less stuff at the beginning, so I picked one that was less at the start of the function)
    Code:
    void N_sentence(FILE *fptr,int nsent)
    {
    
    char fullstop ='.';
    char numb_sentences;
    int s_count=0;
    
    
    
    fptr=fopen("lammin.txt","r");
    Obviously, if you should not OPEN another file on top of the one you have opened in main - it serves no purpose.

    However, as you are reading through the entire file, you will need to get back to the beginning of the file. rewind(fp) or seek(fp, 0, SEEK_SET) will do that for you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    so what you are saying is, (1) I should remove the fopen statement from within all 6 functions, (2) use rewind() in each function to get back to the beginning of the text file, (3) and I an not sure how to pass the point to the function call in the main program.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by willie View Post
    so what you are saying is, (1) I should remove the fopen statement from within all 6 functions, (2) use rewind() in each function to get back to the beginning of the text file, (3) and I an not sure how to pass the point to the function call in the main program.
    Correct on all three accounts, but I guess that 3 is more of a question. Just pass fptr as it is - no *, & or other symbol.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    10
    and also I would say, my biggest problems right now, are the function prototype and definition relating to function pointers.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by willie View Post
    and also I would say, my biggest problems right now, are the function prototype and definition relating to function pointers.
    Function pointers? I thought we were talking about FILE pointers... ??

    The prototypes you have look about right, but some of it looks like you are intending to return a value back through a parameter, e.g:
    Code:
    void Keyword(FILE*fptr,int k_count)
    {
    	
    	char key_temp [25];   /*holds each string in the text file to be compared with keyword*/
    	int k_word=0;         /*k_word is used as a counter and is initialized to zero*/
    	int i;                /*i checks each character*/
    	char keyword [25];    /*keyword enter by user*/
    As it stands, "k_count" isn't being used in the function. Is it your intention that you want to pass back the number of times the keyword was found in that variable? In that case, you would have to use the same technique as when you use scanf - pass the ADDRESS of the counter in your calling code, and use *k_count in the function itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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