Click here to Skip to main content
13,001,686 members (103,542 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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];
        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 
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 29-Apr-13 23:57pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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".
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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:

const char* RegID = "10";
const char* mndtime  = "10";
const char* resourcetype  = "Backup";
const char* ressubtype = "shadowprotect";
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You could make this so much simpler by using an XML writer. XmlLite[^] is a good basic one that you can use in C++.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100