Click here to Skip to main content
15,868,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just want write blank line into the file. i use following code but is not working.
C++
char* RegID;
        RegID = "10";
        char* mndtime;
        mndtime  = "10";
        char* resourcetype;
        resourcetype  = "Backup";
        char* ressubtype;
        ressubtype = "shadowprotect";
        char* DataBuffer = new char[100];

        StrCpy(DataBuffer,"<wpshadowprotectstatus>");
        strcat(DataBuffer,"\n");
        strcat(DataBuffer,"<mndtime>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\mndtime>\n");
        strcat(DataBuffer,"<resourcetype>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\resourcetype>\n");
        strcat(DataBuffer,"<ressubtype>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\ressubtype>\n");
        strcat(DataBuffer,"<jobname>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\jobname>\n");
        strcat(DataBuffer,"<jobstarttime>");
        strcat(DataBuffer,RegID);
        strcat(DataBuffer,"<\\jobstarttime>\n");
        HANDLE hFile;

        hFile = CreateFile("text.txt",                // name of the write
                           GENERIC_WRITE,          // open for writing
                           0,                      // do not share
                           NULL,                   // default security
                           CREATE_NEW,             // create new file only
                           FILE_ATTRIBUTE_NORMAL,  // normal file
                           NULL);                  // no attr. template

        if (hFile == INVALID_HANDLE_VALUE)
        {
            return 0;
        }
        DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
        DWORD dwBytesWritten = 0;
        BOOL bErrorFlag = FALSE;
        bErrorFlag = WriteFile(hFile,           // open file handle
                        DataBuffer,      // start of data to write
                        dwBytesToWrite,  // number of bytes to write
                        &dwBytesWritten, // number of bytes that were written
                        NULL);            // no overlapped 
structure
but i dont known why new line is not dump in text file.

Note :- 1)I dont want to use std:: library c++. 2)Dont want to use xml parser.
Posted

One source of the problem can be that you are writing to the file in binary mode. If you use the std c library and open the file in text mode ("w") then the newline characters ('\n') are automatically converted to platform dependent newlines ("\r\n" on windows). If you write to a file in binary mode then only the '\n' is written. Some editor/viewer programs accept both newlines (windows and unix) (notepad++) but some don't (for example windows notepad). You could try to fix your problems by writing out "\r\n" instead of "\n".
 
Share this answer
 
The new lines are almost certainly being written to the file, but I suspect the editor doesn't recognise them as a full line break. Are you using notepad by any chance?

If you append "\r\n" instead of "\n" then it should be visible as a line break in notepad.

However .....

1. You do know you have a buffer overrun in DataBuffer, the amount of text you are writing to is clearly over 100 characters. This could result in some odd behaviour.

2. If you are writing out XML data then the closing tags should be forward not backslash, and you do not close the wpshadowprotectstatus tag.

3. I presume you are deleting the buffer and closing the HFILE handle at the end of it's lifetime?

You should also probably declare the const strings as:

C++
const char* RegID = "10";
const char* mndtime  = "10";
const char* resourcetype  = "Backup";
const char* ressubtype = "shadowprotect";
 
Share this answer
 
v2
You could make this so much simpler by using an XML writer. XmlLite[^] is a good basic one that you can use in C++.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900