Вопрос

I just want write blank line into the file. i use following code but is not working.

        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.

Это было полезно?

Решение

Use \r\n for line breaks on Windows.

And your XML is malformed. XML closing tags use the / character, not the \ character. And you are writing the same RegID variable for all of the XML values instead of using your other variables (mndtime, resourcetype, etc).

Другие советы

Windows? If so, replace \n with \r\n. For FILE* / iostream it is done automatically by runtime, but not for WriteFile. And, of course, you need two line endings to get blank line.

BTW, generating long string with strcat has O(N^2) complexity, which is very bad.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top