我只想在文件中写入空白行。我使用以下代码,但无法正常工作。
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
但我不知道为什么新行不会在文本文件中转储。
注意: - 1)我不想使用std :: library c ++。 2)不想使用xml解析器。
答案 0 :(得分:2)
窗? 如果是这样,请将\ n替换为\ r \ n。对于FILE * / iostream,它由运行时自动完成,但不适用于WriteFile。 当然,你需要两行结尾才能得到空白。
BTW,使用strcat生成长字符串具有O(N ^ 2)的复杂度,这非常糟糕。答案 1 :(得分:2)
在Windows上使用\r\n
换行。
您的XML格式错误。 XML结束标记使用/
字符,而不是\
字符。并且您为所有XML值编写了相同的RegID
变量,而不是使用其他变量(mndtime
,resourcetype
等)。