- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,每次调用时都会将文本插入到文件中。我不想重写整个文件,我希望新文本可以插入到新行中。这是我的测试代码:
void writeFile()
{
FILE *pFile;
char* data = "hahaha";
int data_size = 7;
int count = 1;
pFile = fopen("textfile.bin","a+");
if (pFile!=NULL)
{
fwrite (data, data_size, count, pFile);
fclose (pFile);
}
}
第一次调用时,一切正常。新建了一个文件,数据写入成功。但是当我再次调用它并期望插入新数据时,我在文件中得到了奇怪的字符串,例如:怜惜栀桡桡a。
我不太熟悉 C++ I/O 函数。有人可以告诉我我做错了什么吗?另外,对于将文本附加到下一行有什么建议吗?
最佳答案
我认为您遇到了代码集问题,您用来查看您编写的文件的程序希望在文件中找到 UTF-16 数据。
我基于对您引用的字符串的分析:
慨慨慨栀桡桡a
当该 (UTF-8) 数据转换为 Unicode 值时,我得到:
0xE6 0x85 0xA8 = U+6168
0xE6 0x85 0xA8 = U+6168
0xE6 0x85 0xA8 = U+6168
0xE6 0xA0 0x80 = U+6800
0xE6 0xA1 0xA1 = U+6861
0xE6 0xA1 0xA1 = U+6861
0x61 = U+0061
0x0A = U+000A
Unicode 值 U+6168 以小端字节序表示为字节 0x68 0x61,h
的 ASCII 码为 104 (0x68),a
为 97 (0x61)。因此,数据可能写入正确,但对写入数据的解释不正确。
正如我在 comment 中指出的那样:
If you want lines in the file, you'll need to put them there (by adding newlines to the data that is written), because
fwrite()
won't output any newlines unless they are in the data it is given to write. You have written a null byte to the file (because you useddata_size = 7
), which means the file is not really a text file (text files don't contain null bytes). What happens next depends on the code set you're using.
输出中出现尾随单字节代码,因为第二个空字节在此页面上粘贴的内容中不可见,而尾随 U+000A 是由命令中的 echo
添加的我用于分析的行(其中 utf8-unicode
是我编写的程序):
echo "慨慨慨栀桡桡a" | utf8-unicode
关于c++ - 尝试使用模式为 "a+"的 fwrite() 将新文本附加到现有文件,但写入了奇怪的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927261/
我遇到一些 fwrite 问题,它以我不明白的方式失败。 在这段代码中,fwrite 都失败并给出 tmp = 0。 if ((file = fopen(filenameout, "wb")) !
好吧,事情是这样的……我记得上周创建了一个程序,要求我以二进制模式打开一个文件并向其中写入数据。首先,我尝试使用fopen函数,检查结果是否正确,然后尝试写入数据。我记得第一次尝试时,fwrite 操
早上好, 我正在尝试将以下结构的内容写入文件(更具体地说是 BMP 文件),并且想知道为什么 fwrite 的二进制输出ing 整个结构和 fwrite结构体的各个项目是不同的。这就是C的工作方式吗?
我在某个地方遇到了这个。 正在写入一个文件,然后rewind(fileptr) 完成,然后写入应该写入文件开头的内容。但是写在开头,不会覆盖文件的内容吗?请指教。 最佳答案 技术上,是 是的,文件将被
fwrite 一个整数取决于字节序,但是有没有一种方法可以将一个整数 0x00000004 写入一个文件,这样无论它运行在什么机器上,它都可以始终被读取为 0x00000004。 一个想法是始终按照特
我在 php 中编写一个函数,在客户端我有一个 Canvas 图像,我使用 toDataUrl() 和一个文件名将图像保存在服务器上。这是代码: 关键是这段代码有效。对于我在其上使用的三分之二的页面
当我运行这个小代码并在控制台上输入 3 个整数时,由于 fwrite 语句,它没有按应有的方式打印出来。只有在我连续按回车键一段时间后,它才会打印出来。帮助?P.S:学习速度比 scanf 和 pri
我的网站在 /var/www/html/hs/ ,我有我的更新.php .它应该写到/var/www/html/hs/json/myFile.json ,但事实并非如此。当我尝试写入 的同一目录时更新
这个函数应该获取一个参数作为文件的指针并将所有文件放入struct anagram ,然后将其写入另一个文件。现在每个数据之间都有很大的空间。 charCompare 工作正常,因为我制作了一个测试文
这是在我的网站上上传带有“var$”内容的“file.txt”的 URL: http://www.mywebsite.com/fwrite.php?stringData=var$&myFile=fil
如果字符串包含 ~,我似乎无法将其打印到标准输出。 > A = "/.git". > io:fwrite(A). /.gitok > B = "~/.git". > io:fwrite(B). **
我发现当我尝试写入一些大数据时 fwrite 失败,如下面的代码所示。 #include #include int main(int argc, char* argv[]) { int s
我正在写这个: $fh = fopen('public/newsletter.txt', 'w'); foreach($entries as $row) { fwrite($fh,
我又来了,这次有一个 PHP 问题...我正忙于制作某种管理面板(非常基本),但是当我试图像我的 slider 或其他东西一样进行更改时,fwrite 会覆盖所有内容。这是我的 PHP 代码: 0)
根据fwrite的手册页: fread() and fwrite() return the number of items successfully read or written (i.e., no
我只是想对一组数字进行排序(虽然我发布的代码不是我要使用的方法,但我确实需要将代码中的输出写入二进制文件). 一切正常,除了“ordenar”的最后一次迭代:当我在订购后再次打印回数字时,除了最后一行
我想使用 data.table::fwrite 以文本日志的形式快速存储和检索状态。这些是通过移动应用程序更新的,该应用程序使用管道工 API 调用到 R 端点。移动应用程序每秒可能会触发许多 API
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 5 年前。 Improve th
我正在尝试使用“fwrite”并制作 snd 文件。我想做 IIR 滤波器。我制作了一个 FIR 滤波器,并使用 IIR 滤波器的代码。(当然,改变系数)但我认为“fwrite”不起作用。因为IIR滤
我正在尝试将 wchar 数组写入 C 中的文件,但是存在某种损坏和不相关的数据,例如这样的变量和路径 c.:.\.p.r.o.g.r.a.m. .f.i.l.e.s.\.m.i.c.r.o.s.o.
我是一名优秀的程序员,十分优秀!