Thread: Multi line quotes used on single lines

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    14

    Multi line quotes used on single lines

    Working with other peoples code where they have used quotes in the style of /*,*/ for singles really grates on me e.g.:

    print( "Hello World\n" );/*some pointless quote*/

    I often want to comment out large blocks of code using /*,*/ but cant because it closes the comment after the first */ it encounters.

    Does anyone know of an application to remove theses? or an application which formats a source file based on a template you can design?
    Currently Reading:

    Mathematic from the birth of numbers,
    Effective TCP/IP programming,
    Data Compression: The Complete Reference,
    C Interfaces and Implementations: Techniques for Creating Reusable Software,
    An Introduction to Genetic Algorithms for Scientists and Engineers.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    i cant really understand what do you mean to say

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    try this, copy and paste this in you program and the whole thing will be commented out.
    Code:
    /* comment one at this point hit enter 
    you are now in a new line which is also commented
    hitting enter again you are in the 3rd new line*/
    When no one helps you out. Call google();

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    He means something like nested comments
    Code:
    /*
      jhfgjhfgjfg
      gfdgdf
      /*gdfgdfgdfgdfg*/
      gfdgdfg
    */
    the last line and the last */ is not commented out

  5. #5
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Quote Originally Posted by CBUK
    Does anyone know of an application to remove theses? or an application which formats a source file based on a template you can design?
    you can just use
    Code:
    #if 0
    printf("hello!\n"); /* make sure we are noticed. */
    #endif
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Got it in 1 sandman, it bugged me so much im writing a program to format code in my style.
    Thanks sokra a cleaver little work around. Not a big fan of the pre-processor so tend to avoid it, probably why I didn't think of that.
    Currently Reading:

    Mathematic from the birth of numbers,
    Effective TCP/IP programming,
    Data Compression: The Complete Reference,
    C Interfaces and Implementations: Techniques for Creating Reusable Software,
    An Introduction to Genetic Algorithms for Scientists and Engineers.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's a utility I wrote at some point. You might find it useful:
    Code:
    itsme@dreams:~/C$ cat comments.c
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    int reverse = 0;
    
    int main(int argc, char **argv)
    {
      char *filename;
      FILE *fp;
      char buf[4096], buf2[4096], *p;
      int incomment = 0;
    
      if(argc == 3)
      {
        if(!strcmp(argv[1], "-r"))
          reverse = 1;
        filename = argv[2];
      }
      else if(argc == 2)
        filename = argv[1];
      else
      {
        puts("Usage: comments [-r] <filename>");
        return 1;
      }
    
      if(!(fp = fopen(filename, "r")))
      {
        perror("fopen()");
        return 1;
      }
    
      while(fgets(buf, sizeof(buf), fp))
      {
        if(!incomment && (p = strstr(buf, "//")))
        {
          if(reverse)
          {
            strncpy(buf2, buf, p-buf);
            buf2[p-buf] = '\0';
            puts(buf2);
          }
          else
            fputs(p, stdout);
    
          continue;
        }
    
        for(p = buf;*p;p++)
        {
          if(incomment)
          {
            if(*p == '*' && *(p+1) == '/')
            {
              p++;
              incomment = 0;
    
              if(!reverse)
                puts("*/");
            }
            else
            {
              if(!reverse)
                putchar(*p);
            }
          }
          else
          {
            if(*p == '/' && *(p+1) == '*')
            {
              p++;
              incomment = 1;
    
              if(!reverse)
                fputs("/*", stdout);
            }
            else
            {
              if(reverse)
                putchar(*p);
            }
          }
        }
      }
    
      fclose(fp);
    
      return 0;
    }
    If used on a test file like this:
    Code:
    #include <stdio.h>
    
    // A hello world program
    
    int main(void)
    {
      /* print it */ printf("Hello, world!\n"); /* Done */
    
      return 0; // return to OS
    }
    It produces output like this:
    Code:
    itsme@dreams:~/C$ ./comments test.c
    // A hello world program
    /* print it */
    /* Done */
    // return to OS
    itsme@dreams:~/C$
    Or like this in reverse:
    Code:
    itsme@dreams:~/C$ ./comments -r test.c
    #include <stdio.h>
    
    
    
    int main(void)
    {
       printf("Hello, world!\n");
    
      return 0;
    }
    itsme@dreams:~/C$
    You, personally, might find it useful by running it like: comments -r file.c > filenocomments.c
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    14
    Thanks itsme86, my grumble inspired me to start writing y own until that does something similar to yours. It checks if a '\n' is in a comment in the style of /**/ and if its not removes the training*/ and replaces the opening /* with //.
    Currently Reading:

    Mathematic from the birth of numbers,
    Effective TCP/IP programming,
    Data Compression: The Complete Reference,
    C Interfaces and Implementations: Techniques for Creating Reusable Software,
    An Introduction to Genetic Algorithms for Scientists and Engineers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi line
    By Salibea in forum Windows Programming
    Replies: 11
    Last Post: 08-11-2005, 04:00 AM
  2. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  3. concatenating single chars to multi char arrays
    By TJJ in forum C Programming
    Replies: 7
    Last Post: 11-20-2003, 04:09 AM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM