在文件中写入新的行字符
Writing new line character in file
我只想在文件中写入空白行。我使用以下代码,但不工作。
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解析器
在Windows上使用rn
作为换行符
您的XML格式是错误的。XML结束标记使用/
字符,而不是字符。并且您正在为所有XML值编写相同的
RegID
变量,而不是使用其他变量(mndtime
, resourcetype
等)。
Windows?如果是,将n替换为rn。对于FILE*/iostream,它会在运行时自动完成,但对于WriteFile则不会。当然,你需要两个行尾来得到空行。
顺便说一句,用strcat生成长字符串的复杂度是0 (N^2),这是非常糟糕的。
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 如何从文本文件中读取数值,直到遇到字符类型?
- 使用 fopen 打开 .pak 文件并使该文件应用于 const 无符号字符* (C++)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 尝试将字符串从文件读取到无符号字符向量中
- 如何在C++中反转文本文件中字符的顺序?
- 生成随机.txt文件,10行,10列。充满随机:"*"、"#"、"@"。然后将随机字符替换为'P'
- 如何存储 unicode 字符并将其输出到文件?
- 无法将整个文本文件复制到字符数组
- 仅读取文件末尾的最后 3 个字符,并将其另存为 int
- 将未签名的字符写入输出文件
- 从我的 2DgridArray 中的文本文件中获取每个字符
- ifstream 读取的字符与文件中写入的字符不同
- VSCode C++调试文件名中包含中文字符的文件时插件损坏
- 从文本文件 (C++) 生成字符矩阵
- 使用指针编译代码后,.cpp文件将变为随机字符
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 跨平台UTF-8字符文件数据编码/解码