none
Writing string to file in correct format.

    Question

  • Hi All,

    I have to create a .htm page for which I have all the data stored inside a string and then writes this string to a file. It works fine, but data inside the file comes in a single line. Is there any way to write the string in structured way to a file.

    I have done it like this :-

    main()
    {
    char *strWebPage = "<html>\
    	      <head>\
     <META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\
     <META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=UTF-8\">\
    <title>MyPage</title>\
     <script language=\"JavaScript\">\
     var L_LAUNCHSAP_TEXT = \"Launch stand-alone Windows Media Player\";\
      var g_bNetscape = ( -1 != navigator.appName.indexOf( \"Netscape\" ) );\
    </script>\
    </html>";
    
     FILE *filePtr;
     errno_t err;
     err = fopen_s( &filePtr, "C:\\Test files\\AK_Test.htm","w");
     fputs( strWebPage, filePtr);
     fclose(filePtr);
    }
    

    I want that the AK_Test.htm file will show its contents in correct manner instead of all the data in a single line.

    I faced similar issue while creating xml files, but that is resolved by using IXMLDOMDocument object. As with the help of this we will load any string in an xml file. Does any such way(interface) exits to do the same for .htm file. IXMLDOMDocument can`t be used as this require correct xml format and my string contains some scripts etc. which does not lie in xml format and as a result load function fails.

    Help me out on this.
           

    Thursday, July 28, 2011 1:47 PM

Answers

  • You need to do it this way -
    char* strWebPage = "<html>"

     "<head>"

    .

    .

    .

    Though correct, in this case "<html"> and "<head>" will appear on the same line.

    One can do:

    char* strWebPage = "<html>\n"

    "<head>\n"

    .

    .

    .

     


    James A. Chappell
    • Marked as answer by Aneeque Friday, July 29, 2011 9:41 AM
    Thursday, July 28, 2011 2:52 PM

All replies

  • Place \n where you want new lines.
    James A. Chappell
    Thursday, July 28, 2011 1:58 PM
  • HTML doesn't care about its contents being in a single line or on multiple lines.

    The browser will display the contents the same in both ways.

    The problem is that you have a backslash at the end of each line.

    You need to do it this way -

    char* strWebPage = "<html>"

    "<head>"

    .

    .

    .


    «_Superman_»
    Microsoft MVP (Visual C++)

    Polymorphism in C
    Thursday, July 28, 2011 2:17 PM
  • You need to do it this way -
    char* strWebPage = "<html>"

     "<head>"

    .

    .

    .

    Though correct, in this case "<html"> and "<head>" will appear on the same line.

    One can do:

    char* strWebPage = "<html>\n"

    "<head>\n"

    .

    .

    .

     


    James A. Chappell
    • Marked as answer by Aneeque Friday, July 29, 2011 9:41 AM
    Thursday, July 28, 2011 2:52 PM
  • You need to do it this way -
    char* strWebPage = "<html>"

     "<head>"

    .

    .

    .

    Though correct, in this case "<html"> and "<head>" will appear on the same line.

    One can do:

    char* strWebPage = "<html>\n"

     

     


    James A. Chappell

     

    "<head>\n"

    .

    .

    .

     


    Thanks, putting "\n" works for me.

    by doing this way

              char* strWebPage = "<html>"

     

             "<head>"

            .

    all the data will remain in the same line. 


    • Edited by Aneeque Wednesday, August 03, 2011 9:54 AM make some corrections to the post
    Friday, July 29, 2011 9:48 AM
  • It shouldn't.

    The language has a feature that causes adjacent string literals in the source to be concatenated during compile time into a single string literal in the object file.

    char *x = "abcd\
    efgh";

    will generate the exact same string literal for x to point to as

    char *x "abcd"
          "efgh";


    Both generate the literal "abcdefgh"

    Note that intervening white space between the quoted substrings is not include in the string in the second example.  In the first example, if efgh were not at the start of the next line, then any characters including spaces would be included in the string (as in "abcd     efgh").

    The presence of \n escape sequences inside the quotes has no effect other than to include the appropriate 0x0a bytes in the resulting literal.  When the literal is written to a file, the 0x0a bytes will be expanded to the usual 0x0a0d pairs.

    Sunday, July 31, 2011 7:30 PM
  • I made some corrections to my earlier post (removed \n from the example).

    Barry, you are right, both of your examples will generate the same literal "abcdefgh".

    Putting \n for examples :-

    char *x = "abcd\n"

    "efgh";

    will result each substring in new line and generate the following result :-

    abcd

    efgh

    as " 0x0a" is equal to the ASCII code for LF (Line Feed), which is a newline on many systems.

    Wednesday, August 03, 2011 11:38 AM