Thread: find and replace command?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    6

    find and replace command?

    I know it should be simple, but I am sooo bad at this... could someone give a simple example of a find and replace command that takes arguments? I kno it should be something along the lines of (P.S. this came from another thread, and was Quzah's code:
    #include <stdio.h>
    int main (char *filename)
    {
    FILE *fp = NULL;
    int c = 0;

    if( (fp = fopen(filename, "r" )) != NULL)
    {
    while( (c = fgetc( fp )) != EOF )
    {
    if( c == '<' )
    {
    do printf( "%c", c ); while( (c = fgetc( fp )) != EOF && c != '>' );
    printf(">\n");
    }
    }
    fclose( fp );
    }
    else
    printf("Error opening file.\n");
    return 0;
    }

    /**** IT IS QUZAH'S CODE! NOT MINE! (of course, I could never program something so simple and fast....)****/

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Did you bother to look at this in your other thread:
    Code:
    #include <stdio.h> 
    
    int main (int argc, char * argv[]) 
    { 
    	FILE * fp = NULL;
    	int c = 0; 
    
    	if (argc == 2)
    	{
    		if( (fp = fopen(argv[1], "r" )) != NULL) 
    		{ 
    			while( (c = fgetc( fp )) != EOF ) 
    			{ 
    				if( c == '<' ) 
    				{ 
    					do printf( "%c", c );
    					while( (c = fgetc( fp )) != EOF && c != '>' ); 
    					printf(">\n"); 
    				} 
    			} 
    			fclose( fp ); 
    		} 
    		else 
    		printf("Error opening file.\n"); 
    	}
    	/* remove this if you do not with to see the path to your program */
    	printf(argv[0]);
    	return 0; 
    }
    Refer to this for more:
    Code:
    /*
    
    	Program:
    	CmdLine.c
    	
    	Purpose:
    	Demonstrates the use of command line parameters.
    
    	argc = command line argument count
    	
    	argv[number] = individual commands read
    	0 = the path to the current program, always a given
    	1 = parameter
    	2 = parateter
    	etc
    	
    */
    
    #include <stdio.h>
    
    void default_message(void);
    
    int main( int argc, char * argv[] )  
    {
    	if (argc == 2)
    	/* if there was a parameter besides the path to this program */
    	{
    		if(argv[1][0] == '?')
    		/* if the parameter was a question mark */
    		{
    			printf("Help stuff would go here.\n");
    		}
    		else
    		/* if the parameter was not a question mark, print whatever it was */
    		{
    			printf("%s \n", argv[1]);
    		}
    	}
    	else if ( argc >= 3 )
    	/* if there were more than 2 parameters besides the path to the program */
    	{
    		printf("Invalid input:\n");
    		printf("Only one command-line parameter allowed.\n");
    	}
    	else
    	/* if there were no paramters at all */
    	{
    		default_message();
    	}
    	return 0;
    }
    
    void default_message(void)
    {
    	printf("CmdLine.exe:\n");
    	printf("The purpose of this program is to demonstrate command line arguments.\n");
    	printf("This program will print whatever you type on the screen.\n\n");
    	printf("Example:\n");
    	printf("C:\\>CmdLine.exe Hmmm\n");
    	printf("Hmmm\n");
    }
    Use code tags.
    Last edited by Shadow; 05-24-2002 at 11:27 PM.
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    6
    Thx....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find and replace a string in a file
    By salzouby in forum C Programming
    Replies: 13
    Last Post: 09-14-2010, 08:55 AM
  2. Find and replace within a string?
    By clancyPC in forum C Programming
    Replies: 3
    Last Post: 03-20-2009, 07:28 AM
  3. please help!...find and replace
    By amy589 in forum C++ Programming
    Replies: 26
    Last Post: 10-13-2006, 06:42 PM
  4. Find and replace
    By BobDole1 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2003, 10:06 PM
  5. Find and Replace Text Function
    By mart_man00 in forum C Programming
    Replies: 45
    Last Post: 03-13-2003, 10:21 PM