Removing text between /* */ in a file

This is a discussion on Removing text between /* */ in a file within the C Programming forums, part of the General Programming Boards category; Well, the program is designed to remove any text between the /* and */ in a text file input and ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    173

    Removing text between /* */ in a file

    Well, the program is designed to remove any text between the /* and */ in a text file input and also to give output of the original text. Copying the original text was easy, but I'm having trouble 'removing' text in between the /* and */ in a text file.

    Say for example, a.txt had this:
    /* Blah2 */ text /* /* Blah */ */

    The output should be:

    text /* Blah */

    Heres wat I've done so far, I'm having trouble checking for /* and */ since they are 2 characters and getc() only returns one.. so any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // Formatting Constants
    #define LINE "----------------------------"
    
    // FILE* is a pointer variable
    void original(FILE*);
    void extractor(FILE*);
    
    int main(int argc, char **argv)
    {
    	// Point to a File
    	FILE *fp;
    	fp = fopen(argv[1],"r");
    
    	if (argc != 2)
    	{
    		printf("Please input a text file (one is required no more)\n");
    	}
    	else 
    	{
    		fprintf(stdout, "Removing /* and */ signs from %s...", argv[1]);
    		// Check for contents in file
    		fprintf(stdout, "\n");
    		fprintf(stdout, "%s\n",LINE);
    		fprintf(stdout, "Original contents of: %s\n",argv[1]);
    		// Print out original contents
    		original(fp);
    		// Print out extracted (modified) contents
    		fprintf(stdout, "%s\n",LINE);
    		fprintf(stdout, "Extracted contents of: %s\n",argv[1]);
    		extractor(fp);
    		// Close file
    		fprintf(stdout, "%s\n",LINE);
    		fclose(fp);		
    	}
    
    	return 0;
    }
    
    void original(FILE *fp)
    {
    	int c;
    	while ((c = getc(fp)) != EOF) 
    	{
    			putchar(c);
    	}
    	return;
    }
    
    void extractor(FILE *fp)
    {
    	int c;
    	while ((c = getc(fp)) != EOF)
    	{
    		
             /************************/
             /* NEED TO FINISH THIS PART */
             /************************/
    
    	}
    	return;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Basically, you want to update a boolean flag inside that loop
    int inComment = 0;

    Some conditions inside the loop will make inComment = 1, and others will make it inComment = 0

    You output a char when
    if ( !inComment ) putchar( c );

    Start by writing some code which recognises when a / is followed by a *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    27
    Hi,

    Looking at your string

    /* Blah2 */ text /* /* Blah */ */

    Shouldn't the output be

    text */


    because

    /* Blah2 */ text /* /* Blah */ */
    ********---------------------------- is the first pair of /* */

    /* Blah2 */ text /* /* Blah */ */
    ---------------**********------is another pair of /* */

    This leaves "test" and the last "*/"


    Just wondering.
    Last edited by Pappy1942; 04-05-2004 at 08:57 AM.
    Pappy
    You learn something new everyday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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