Thread: help can't find a way to make this work

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    help can't find a way to make this work

    hi, I'm doing a very simple program in which I want to remove comments from a .c file, so I started with something simple and tried to make a program that changed ' ' to a '.' except that I can't make it work, every time the program finds a ' ' it ends the search cycle and the '.' is somehow written at the end of the file. :S It must have a very simple solution please help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *file;
    int o = 1;
    
    void time()
    {
    	int i;
    	for(i=0; i<10000000; i++);
    }
    
    
    void commentremover(FILE *tmp)
    {
    	char o;
    	int i = 0;
    	char input[] = ".";
    	//FILE *tmp2;
    	//tmp2=tmp;
    	while((o=fgetc(tmp)) != EOF)
    	{
    	if(o=='\n')
    	{
    		printf(" %d \t:*LF*:\n", i , o);
    	}
    	else
    	{
    		if(o=='\x0D')
    		{
    			printf(" %d \t:*CR*:\n");
    		}
    		else
    		{
    			if(o==' ')
    			{
    				fwrite(input,1,1,tmp);
    			}
    			else
    			{
    			printf(" %d \t:%c:\n", i , o);
    			}
    		}
    	}
    	i++;
    	fflush(stdout);
    	}
    }
    
    void printer(FILE *tmp)
    {
    	char o;
    	o = fgetc(tmp);
    	while(o != EOF)
    	{
    		time(1);
    		printf("%c", o);
    	o = fgetc(tmp);
    	}
    }
    
    int main()
    {
    	file = fopen("out.txt", "r+");
    	commentremover(file);
    	return 0;
    }

    edit: The printf sequence is part of the 'seach throu the file bit' they will be removed after I get the writing working
    Last edited by Pe6r0; 12-02-2006 at 04:07 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char o;
    /* ... */
    while((o=fgetc(tmp)) != EOF)
    EOF is an int value, and as such should be stored in an int.

    Calling a function time() is a bad idea, because there's a function of the same name in the standard library. In any rate, your time() function takes no parameters, so passing it one as printer() does is a bad idea.
    Code:
    time(1);
    In between reads and writes of a FILE* file, you need to fflush() it. fflush(file), not fflush(stdout).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read up on finite state machines.

    You basically have two states
    - in a comment
    - not in a comment.

    You also have two transitional states
    - entering a comment
    - leaving a comment.

    You decide these things based on finding a / followed by a * and a * 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.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    thanks for all the input ppl! it helped a lot

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    well I did do a fflush of the file between read and write but the end result was the same. I keep getting the stuff written at the end only

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    FILE *file;
    
    void commentremover(FILE *tmp)
    {
    	char o;
    	int i = 0;
    	char input[] = ".";
    	while((o=fgetc(tmp)) != EOF)
    	{
    	if(o=='\n')
    	{
    		printf(" %d \t:*LF*:\n", i , o);
    	}
    	else
    	{
    		if(o=='\x0D')
    		{
    			printf(" %d \t:*CR*:\n");
    		}
    		else
    		{
    			if(o==' ')
    			{
    				fflush(tmp);
    				fopen("out.txt", "r+");
    				fwrite(input,1,1,tmp);
    			}
    			else
    			{
    			printf(" %d \t:%c:\n", i , o);
    			}
    		}
    	}
    	i++;
    	fflush(stdout);
    	}
    }
    
    int main()
    {
    	file = fopen("out.txt", "r+");
    	commentremover(file);
    	return 0;
    }

    I havent reached the part of recognizing a comment yet since my first task would be to succesfully identify a character and substitute it for any other.

    Code:
    			if(o==' ')
    			{
    				fflush(tmp);
    				fopen("out.txt", "r+");
    				fwrite(input,1,1,tmp);
    			}
    EDIT: reseting the pointer tmp seems like a bad ideia since we do need to know where in the file we need to write. So I dropped that ideia. Any other thoughts? I also used fputc which produced the same result. I read the help files (man) on both functions and since I'm not using append mode the only other reason that comes to mind would be that something goes wrong with the writing (fputc writes in append mode when that happens). The ideia of this test part would be to substitute the spaces with dots in a .txt file. I also made some printf to monitor the advance of the search across the file (it stops at the first space)

    out.txt:
    Code:
    Beggining of the file
    Second line
    
    /* comment one */
    
    // line comment
    
    END OF FILE
    I get at stout (after I disregard the fflush and the fopen):

    0 :B:
    1 :e:
    2 :g:
    3 :g:
    4 :i:
    5 :n:
    6 :i:
    7 :n:
    8 :g:

    and ends there
    Last edited by Pe6r0; 12-02-2006 at 09:39 PM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    if(o==' ')
    			{
    				fflush(tmp);
    				fopen("out.txt", "r+");
    				fwrite(input,1,1,tmp);
    			}
    What does the selected line do here?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    As I said I already disregarded the second fopen (it was suggested earlier) that line is now:

    Code:
    			if(o==' ')
    			{
    			fwrite(input,1,1,tmp);
    			}

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    did you see Salems post. It gives u the basic idea of what to do and how to approch.

    1. loop around the file reading each char
    2. check is that char read is '/' if so check for next char read is '/' if so then just discard the line which mean that line is been commented.

    this methods is for the // commenting method. Try solving this problem first

    ssharish2005

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    I did read it My plan is to make a program that searches throu the comments AND removes them. The searching part is easy since I already have the program searching for specific characters. My problem is that I can't write where I want! that program writes always at the end and exits the cycle when I find a "key" character

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    My problem is that I can't write where I want
    REad all file into buffer long enogh
    Modify the buffer in the memory
    Write the resulting buffer to the disk
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    THANKS! I never even thought about it in that way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Football team league need help to make this work
    By wurzull in forum C Programming
    Replies: 1
    Last Post: 04-07-2007, 07:35 AM
  3. MMO Theory, How to make Gameloop & Sockets both work?
    By Zeusbwr in forum Game Programming
    Replies: 3
    Last Post: 08-01-2005, 12:29 PM
  4. Replies: 3
    Last Post: 05-07-2005, 01:52 AM
  5. Listview sorting...Can't make it work!
    By knutso in forum Windows Programming
    Replies: 4
    Last Post: 10-05-2003, 12:48 PM