Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Win32 Text
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.
Posted 30-Apr-13 0:57am
Rate this: bad
good
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".
  Permalink  
Rate this: bad
good
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";
  Permalink  
v2
Rate this: bad
good
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++.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 PIEBALDconsult 110
3 Richard MacCutchan 105
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
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