Thread: Help finding solution

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    Help finding solution

    Hello guys In K&R chapter one there was some couple of stuff to solve like making a program that remove comments from c program i got alrdy my pseudo code done for it
    but problem is how can i read a exe ?like is it possible because when i open a txt of exe it appears as encrypted txt any ideas?

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    They probably mean remove the comments from the source code, not the executable?
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No doubt. And it's not too difficult: print text until you find a "/*", then stop printing until you find a "*/". Single-line comments are much the same.
    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.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i dunno they didnt write but is it possible to remove it from exucatable ?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    yah i did it but i m not sure they asked for .c file
    Code:
    #include <stdio.h>
    void anylze_it(char *buffer);
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
    	{
            anylze_it(buffer);
    		puts(buffer);
    		getchar();
    	}
    }
    void anylze_it(char *buffer)
    {
         int i;
         for(i=0;buffer[i]!='\0';i++)
             if(buffer[i]=='/')
                 buffer[i]=0;
    }
    int main(void)
    {
    	char buffer[100];
    	if(!read_line(buffer,sizeof buffer))
    		exit(0);
        return 0;
    }

  6. #6
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Comments aren't compiled, they are ignored by the compiler, so you can't find them in the executable.

    In your code, you only look for a single '/', but what if it isn't followed up by another '/' ? Or what if it's in a string? And you don't look for the multiline comment '/*'.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are deleting characters following a forward slash, not a comment. For instance, what happens when you have a line of code like:
    Code:
    float result = first_num / second_num;
    There is no comment here, yet your code will delete half of the line.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh yah ur right i got a idea in how to fix i think

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i tried but program didnt work i don't know why though
    Code:
    #include <stdio.h>
    void anylze_it(char *buffer);
    void delete_it(char *buffer,int start);
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
    	{
            anylze_it(buffer);
    		puts(buffer);
    		getchar();
    	}
    }
    void anylze_it(char *buffer)
    {
         int i,x=0;
         for(i=0;buffer[i]!='\0';i++)
             if(buffer[i]=='/')
                 x++;
                 if(x>1){
                     printf("%d",i);
                     delete_it(buffer,i); }
    }
    void delete_it(char *buffer,int num)
    {
         int i;
         for(i=num;buffer[i]!=0;i++)
             buffer[num]=0;
    }
    int main(void)
    {
    	char buffer[100];
    	if(!read_line(buffer,sizeof buffer))
    		exit(0);
    	getchar();
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    i did lil debugging with printf and it printed the num

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    A control path exists such that read_line() does not return a value. In fact the control path is for the normal case.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by laserlight View Post
    A control path exists such that read_line() does not return a value. In fact the control path is for the normal case.
    what you mean i don't understand ?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Look:
    Code:
    int read_line(char *buffer,int num)
    {
        FILE *fp;
        fp=fopen("D:\\finizhad fbot\\8.11.2008\\8.11.2008\\h.c","r");
        if(fp==NULL) return 0;//program failed
        while(fgets(buffer,num,fp)!=NULL)
        {
            anylze_it(buffer);
            puts(buffer);
            getchar();
        }
    }
    Suppose fgets(buffer,num,fp) returns NULL. What does read_line return?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    oh i didnt set it to return anything i will fix that but first i want to get my code working first then do error chking etc l8er on

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    void anylze_it(char *buffer)
    {
         int i,x=0;
         for(i=0;buffer[i]!='\0';i++)
             if(buffer[i]=='/')
                 x++;
                 if(x>1){
                     printf("%d",i);
                     delete_it(buffer,i); }
    }
    Still some problems here. You are checking for two forward slashes in a line, but those slashes can exist anywhere. For instance:
    Code:
    float result = first_num / second_num / third_num;
    Your code will delete everything after the second slash. Note that you never reset x back to zero. Also, you are missing some braces to match your indentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 09:50 AM
  2. anyone know the solution?
    By heeroyung in forum C Programming
    Replies: 15
    Last Post: 09-30-2005, 06:46 AM
  3. Help finding solution to illegal operator
    By nickadeemus2002 in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2004, 10:32 PM
  4. My Unix/Linux SECURITY SOLUTION - pls read
    By bjdea1 in forum Linux Programming
    Replies: 3
    Last Post: 04-11-2004, 09:28 PM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM