【发布时间】:2013-04-30 09:55:23
【问题描述】:
我只想在文件中写入空行。我使用以下代码但不工作。
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解析器。
【问题讨论】:
-
顺便说一句..你不是说 strcat(DataBuffer,"\n");
-
你在文本文件中得到了什么?
-
我怀疑问题在于您使用
CreateFile和WriteFile创建和写入文件。这将避免 C 运行时库为您执行的从'\n'到"\r\n"的自动转换。您很可能会单独获得一个换行符'\n',这意味着一行文本后面紧跟着右下方的下一行。 -
希望这不是您的真实代码,因为您为 100 个字符分配了空间,但看起来您正在编写大约 160 个字符。