c and html

This is a discussion on c and html within the C Programming forums, part of the General Programming Boards category; hi i am writting a program that takes in the input of your html, and proceses it into a .html ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    11

    Question c and html

    hi
    i am writting a program that takes in the input of your html, and proceses it into a .html file called "program". But instead of a code being generated from the user, it just makes one character appear. can someone please help me? heres the code:

    Code:
    #include <stdio.h>
    int a;
    
    FILE * tvFile;
    main()
    
    {
    tvFile = fopen("c:\\program.html", "w");
    
    printf("Type in you html, this will then be put into the directory c:\\program.html");
    scanf("%c",&a);
    char html=a;
    
    
    fprintf(tvFile,"%c",html);
    
    fclose(tvFile);
    return 0;
    
    
    }
    thanks in advance!

  2. #2
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    You're reading in a single char using
    Code:
    scanf("%c", &a);
    Check out reading in lines of text with fgets().

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heres a hint: To do something more then once use a loop.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    ok i have incoorperated fgets(). AND IT WOKRED!!! but now i want to add soemthing where the user choses the name of the .html file. i have tried this, but it doesnt work, can someine help me imorove this code??

    Code:
    #include <stdio.h> 
    #include <string.h> 
    FILE * tvFile;
    int main()
    {
        char fileName;
        char buf[BUFSIZ];
      char *p;
        
        printf("what do you want the .html file to be saved as?");
         if (fgets(fileName, sizeof(fileName), stdin) != NULL)
    {tvFile = fopen("c:\\>%s<.html",fileName, "w");
    
    
    
      char buf[BUFSIZ];
      char *p;
      
     
      printf ("please enter your html, it will be saved to c://program, max%d characters\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", buf);
        
        fprintf(tvFile,"%s", buf);
    
    fclose(tvFile);
      
      }
      
      return 0;
    }
    Thanks again!

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Code:
    fopen("c:\\>%s<.html",fileName, "w");
    When you see the compiler warnings about too many arguments to fopen(), it should be a big hint. fopen() isn't like the printf() functions. Try using sprintf() to copy the complete filename to a buffer and then passing that buffer to fopen(). Also, filenames can't contain the '>', '<', or '\n' characters.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    When you see the compiler warnings about too many arguments to fopen(), it should be a big hint. fopen() isn't like the printf() functions. Try using sprintf() to copy the complete filename to a buffer and then passing that buffer to fopen(). Also, filenames can't contain the '>', '<', or '\n' characters
    sorry i didnt explain...... i am really new to c, so please can you "dumb" that down a bit please. Thanks
    Last edited by spongebob; 07-05-2006 at 10:50 AM. Reason: didnt do the [/quote]

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    owwww im really not understanding this. can somone explain it to me in lamens terms. please? im not in a understanding of sprintf at all! why wont the filename thing work? can someone please explain all of this if possible?

  9. #9
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by spongebob
    sorry i didnt explain...... i am really new to c, so please can you "dumb" that down a bit please. Thanks

    You cannot use print format specifiers (i.e. "%s") in the file name argument to fopen.

    What itsme86 was saying was to create the filename string, using sprintf, and then pass that string as an argument to fopen.

    hope this helps,
    frank
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    sprintf() is just like printf(), only instead of spitting the output to stdout, it spits it into a buffer. Try this program:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[20];
    
      sprintf(buf, "C:\\%s%d", "itsme", 86);
      puts(buf);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    ok i think im starting to get there. can somone give me some example code? nothing to big, just something that demonstrates. It would be highly appriciated! Thanks in advance! oh sorry i didnt see the above!

  12. #12
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125

    more.....

    BTW, there are other errors with your program.....

    Code:
       char fileName;
        char buf[BUFSIZ];
      char *p;
        
        printf("what do you want the .html file to be saved as?");
         if (fgets(fileName, sizeof(fileName), stdin) != NULL)
    {tvFile = fopen("c:\\>%s<.html",fileName, "w");
    is not going to work. Your are only allocating one character's worth of space for the filename.

    Without getting into all the details about dynamically allocating space for user input, you need to at least do something like

    char fileName[256]

    or something like that. (But beware, if the user enters more than that you will buffer overflow).
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  13. #13
    tao
    tao is offline
    Registered User
    Join Date
    Jun 2006
    Posts
    26
    Some issues to take note:

    1- You're declaring char buf[BUFSIZ] and char *p twice, which isn't necessary (or proper in this case).
    2- You are declaring variable fileName as a single character (read up on arrays if you don't understand them yet). Try declaring fileName as
    Code:
    char fileName[50]
    or an appropriate max size for the filename to be (there's a MAX_PATH or something for an OS's maximum allowable path size, but I forget what the actual parameter is and it might be C++ only).

    > Once you've fixed that up, you can just call
    Code:
    tvFile = fopen(fileName, "w");
    That only works if the user enters a legitimate filepath/name for the file, though. If they just enter a name (i.e. "myfile.txt"), it will create the file in the current working directory.
    Last edited by tao; 07-05-2006 at 11:21 AM.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    11
    i am still extremely stuck. I have now chaneged the number of characters for the filename. But it still isnt allowing me to create the filename. This is partly becuase i am not understanding what you are saying - my fault entirely.

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Let's see your new code.
    If you understand what you're doing, you're not learning anything.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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