Thread: Fgets problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Fgets problem

    i m trying to finish this mail extractor as exersise for FILE I/O i just learned but so far i goten till check for the @ button this how far i have gotten
    Code:
    #include "includes.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #define FNAME "D:\\Karim test folder\\two.html"
    FILE*fptr;
    void close_the_file();
    void pause();
    /*
     *pause()
     *
     *asks the user to press enter
     *this function is used in conjuction with atexit()
     */
    void close_the_file()
    {
    
    	fclose(fptr);
    }
    
    void pause()
    {
    	printf("\nprogram ended ,Please press enter: ");
    	fflush(stdin);
    	getchar();
    }
    int main(void)
    {
    	 char array[600];
    	 int count=0;
    	 unsigned int i;
    	 atexit(pause);
    	 fptr=fopen(FNAME,"r");
    	 if(fptr=NULL)
    	 {
    		 fprintf(stderr,"Coudnt not open \n",FNAME);
    		 exit(1);
    	 }
            atexit(close_the_file);
            printf("The lines are as follows %s",FNAME);
    while(fgets(array,sizeof array,fptr)!=NULL);
    			for( i = 0; i <strlen( array ); i++ )
    			{
    				if(array[i]=='@')
    				{
    					fputs(array,stdout);
    		}
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    program compiles but it gets debug error

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why do you call atexit() twice in main like this?
    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

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah srry mistake should be like this
    Code:
    #include "includes.h"
    int main(void)
    {
    	 char array[600];
    	 int count=0;
    	 unsigned int i;
    	 atexit(close_the_file);
    	 atexit(pause);
    	 fptr=fopen(FNAME,"r");
    	 if(fptr=NULL)
    	 {
    		 fprintf(stderr,"Coudnt not open \n",FNAME);
    		 exit(1);
    	 }
            printf("The lines are as follows %s",FNAME);
    while(fgets(array,sizeof array,fptr)!=NULL);
    
    			for( i = 0; i <strlen( array ); i++ )
    			{
    				if(array[i]=='@')
    				{
    					fputs(array,stdout);
    		}
    }
    return 0;
    }

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I meant: can you explain why you call atexit() twice in main?
    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

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    By the way, you shouldn't need to close the file. C should do that for you automatically.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i did it because it last thing it does is first do pause function
    then closes the file

  8. #8
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by King Mir View Post
    By the way, you shouldn't need to close the file. C should do that for you automatically.
    Are you saying it's not necessary to close the file only in the OP's situation(program)?

    Or do you mean not to use fclose() at all to close files that have been opened with fopen()?
    OS: Linux Mint 13(Maya) LTS 64 bit.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by King Mir View Post
    By the way, you shouldn't need to close the file. C should do that for you automatically.
    No... if your implementation does fine, but it's not portable and it's plain stupid.

    Also don't use fflush(stdin) -- see the FAQ.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lolguy View Post
    i did it because it last thing it does is first do pause function
    then closes the file
    Okay, I didn't realize you could call it twice. I notice that it calls them in order of the most recently recieved call. There are a lot of things to be distracted by in this big chunk of code about fgets.
    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

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oke till now i have gotten till here
    Code:
    #include "includes.h"
    
    int main(void)
    {
    	 char array[600];
         char valid_characters[]="asdfghjklmnbvcxzqwertyuiop";
    	 int count=0;
    	 int i;
    	 int x;
    	 atexit(close_the_file);
    	 atexit(pause);
    	                                                                   /*
    	                                                                    *First,open the text file for writing
    	                                                                    *and write out the header info to it
    	                                                                   */
    	 for(x=0;valid_characters[x]!='\0';x++);
    	 /*{
    		printf("%c\n",valid_characters[x]);
    	 }*/
    fptr=fopen(FNAME,"r");
    if(fptr==NULL)
    {
    	fprintf(stderr,"Coudnt not open \n",FNAME);
                                                                             /*here we test
    																		  *if fopen returns NULL
    																		  *then there is a problem happened
    																		  *we putted them in stderr and say
    																		  *couldnt open file name bla coz bla
    																		  *happened :P*/
    exit(1);
    }
    
    printf("The lines are as follows %s",FNAME);
    /*
    *write the rest of the header info,including current time
    */
    for(;fgets(array,sizeof array,fptr)!=NULL;)
    {
    	for(i=0;i<strlen(array);i++)
    	{
    		if(array[i]=='@' && array[i]==valid_characters[x])
    		{
    			fputs(array,stdout);
    		}
    	}
    }
    return 0;
    }

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What is this:
    Code:
    for(;fgets(array,sizeof array,fptr)!=NULL;)
    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

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    it get array till NULL is reached end of file then it quit

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So what is your problem?
    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

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    well first one didnt work it just pressed all txt :S i need to complete this exersice 2 hours
    is very much for it :S
    Code:
    #include "includes.h"
    
    int main(void)
    {
    	 char array[600];
         char valid_characters[]="asdfghjklmnbvcxzqwertyuiop@";
    	 int i;
    	 int x;
    	 atexit(close_the_file);
    	 atexit(pause);
    	                                                                   /*
    	                                                                    *First,open the text file for writing
    	                                                                    *and write out the header info to it
    	                                                                   */
    fptr=fopen(FNAME,"r");
    if(fptr==NULL)
    {
    	fprintf(stderr,"Coudnt not open \n",FNAME);
                                                                             /*here we test
    																		  *if fopen returns NULL
    																		  *then there is a problem happened
    																		  *we putted them in stderr and say
    																		  *couldnt open file name bla coz bla
    																		  *happened :P*/
    exit(1);
    }
    
    printf("The lines are as follows %s",FNAME);
    /*
    *write the rest of the header info,including current time
    */
    for(;fgets(array,sizeof array,fptr)!=NULL;)
    {
              for(x=0;valid_characters[x]!='\0';x++)
    	{
    	for(i=0;i<strlen(array);i++)
    	{
    	
    		if(array[i]==valid_characters[x])
    		{
    			fputs(array,stdout);
    			Sleep(100);
    		}
    	}
    }
    }
    return 0;
    }


    that first one didnt work out but now i went into for loop i know its almost fixed i just need lil hints thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with fgets....
    By Huskar in forum C Programming
    Replies: 5
    Last Post: 03-29-2009, 10:13 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. print problem while using fgets()
    By learninC in forum C Programming
    Replies: 12
    Last Post: 05-15-2005, 09:29 PM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM