Thread: Printing a double forwardslash...?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Question Printing a double forwardslash...?

    EDIT: new question at the bottom :S

    Hello again, everybody.
    I am trying to write a quick-and-dirty program to format some html. I can print to the file and everything, but my problem is that html often has two successive double-forwardslashes, eg //. How can I get these to print to the file rather than being read as comment marks?

    Thanks
    Last edited by fraktal; 01-17-2006 at 08:11 PM.

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Off the top of my head, won't "\//" work?
    EDIT: Or you could just google an ascii chart and plug in the numbers instead.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    great! I knew it would be simple. thanks.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What's wrong with "//"? Just a regular two forward slashes works for me... I don't think you can start comments inside string literals. This compiled and worked fine (with no errors) for me...

    "\//" doesn't work, however. (Well, it will...) If you use that, you get warned as such:
    Code:
    slashx2.c:5:9: warning: unknown escape sequence '\/'
    This compiles & works fine for me:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	printf("//");
    	return 0;
    }
    Code:
    C:\...>gcc -Wall -O2 -o slashx2.exe slashx2.c
    
    C:\...>slashx2
    //
    C:\...>gcc -v
    ...
    gcc version 3.4.2 (mingw-special)
    Last edited by Cactus_Hugger; 01-18-2006 at 11:41 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Thumbs up

    aha, yes, you are right. just after trying ahluka's suggestion, I realised that the problem wasn't the forwardslashes but the double quotes in <!doctype html public "-//W3C//DTD HTML 4.01//EN">. So I just replaced each " with \" and everything was fine and dandy!

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Unhappy

    Okay, new problem, I'm afraid....

    I have tried the FAQ but the method for changing directories doesn't appear to work. What I want to do is to make a new directory with a name based on a user input.

    I have aquired the filename through fgets so it could be more than one word...would this make a difference? Based on some fiddling I don't think so.. It doesn't seem to make any difference whether or not the name has spaces in.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    ...
    
    fgets(booktitle, sizeof(booktitle), stdin) != NULL;
    
        if ((p = strchr(booktitle, '\n')) != NULL)
                 *p = '\0';      //strips trailing newline
        mkdir(booktitle);
        chdir(booktitle);
    This compiles fine (all variables are declared), but the program crashes at the chdir.

    cheers again...

    EDIT: names of variables.
    Last edited by fraktal; 01-18-2006 at 04:10 PM.

  7. #7
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by fraktal
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    ...
    
    fgets(booktitle, sizeof(booktitle), stdin) != NULL;
    
        if ((p = strchr(booktitle, '\n')) != NULL)
                 *p = '\0';      strips trailing newline
        mkdir(fileoutname);
        chdir( fileoutname );
    This compiles fine (all variables are declared), but the program crashes at the chdir.
    Hmm ... what flavor of unix are you using?
    Code:
    SYNOPSIS
           #include <sys/stat.h>
           #include <sys/types.h>
    
           int mkdir(const char *pathname, mode_t mode);
    In any case, you don't check to see if mkdir() succeeds, which is not wise.

  8. #8
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    I doubt it crashes at chdir(). However, have you checked fileoutname is a valid string? I don't see it anywhere in your code, so perhaps you forgot to initialise it?


    And yes, check if both mkdir and chdir succeed, as suggested.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    Yes, I agree that I need to check whether or not they succeed. At the moment, though, I just want them to work. I am not using UNIX, and I did consider that that could be the problem (ie, those are unix commands, are they not? I just assumed that as well as UNIX they were valid in C, the same way that functions are often named the same in different languages).

    I am working in Windows XP - should I be using windows.h instead? Sorry to keep asking questions that are probably obvious. I have searched and googled and things.

    Basically, all I want to do is take an input, create a directory with the name of that input, and then write a load of files to that directory.
    It *should* be really simple, and I'm sure it is...

    EDIT: I hadn't noticed that I had posted booktitle and fileoutname like that. I was just trying another input method, which I didn't post. Please assume that they are the same variable (i'll edit my original post too)


    [rant]
    incidentally, "C" really is a frustrating name for a programming language. I mean, if I was using visual basic I could google "visual basic" make directory or something, but "c" is such a rubbish keyword.[/rant]
    Last edited by fraktal; 01-18-2006 at 12:43 PM.

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    >> [rant]
    incidentally, "C" really is a frustrating name for a programming language. I mean, if I was using visual basic I could google "visual basic" make directory or something, but "c" is such a rubbish keyword.[/rant]

    Then google "c programming tutorial" - tis what I did.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    old man
    Join Date
    Dec 2005
    Posts
    90
    I assumed you were using unix because you included unistd.h ... these are posix definitions (ie, unix standard functions). I have no idea what meaning they have under winxp. Anyway, since you agree that you need to check for the success of mkdir(), better just do it :-)

    Less haste more speed, and all that.

    Also, how have you declared booktitle? My preference is this way
    Code:
    #define TITLE_SIZE 20
    
    ...
    
    char booktitle[TITLE_SIZE];
    
    ...
    
      fgets(booktitle, TITLE_SIZE, stdin) != NULL;
    Because a reference to an array will decay to a pointer in some cases, which will make "sizeof booktitle" equal the size of a char pointer (usually 4) ...

    Also that last line I've pasted in is wrong anyway since you're not really testing for NULL ... you forgot to phrase it as an "if" ...

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    right, thanks. Bizarrely, mkdir works......go figure. I did declare booktitle (see the edits above) but your way is neater. I'm still stuck as to the directory stuff though....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New To C. Need Help
    By raortega3 in forum C Programming
    Replies: 3
    Last Post: 10-10-2007, 11:24 PM
  2. what this error message says?
    By turkertopal in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2006, 02:44 PM
  3. Rectangular Approximation Program Help
    By Noah in forum C Programming
    Replies: 4
    Last Post: 03-15-2006, 02:23 PM
  4. Please HELP!!
    By traz in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2003, 09:20 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM