C中特殊字符文件的读写

发布于 2024-12-25 02:00:10 字数 576 浏览 0 评论 0原文

使用 C 语言,我尝试操作 openssl 生成的一些文件,其中包含很多(非常)特殊字符。但文件结尾似乎被过早检测到。 例如,请参阅我的程序的摘录,该摘录应该将一个文件复制到另一个文件:(

为了简单起见,我没有显示打开文件的测试,但我在我的程序中这样做)

char msgcrypt[FFILE];
FILE* fMsg = fopen(f4Path,"r");
while(fgets(tmp,FFILE,fMsg) != NULL) strcat(msgcrypt,tmp);
fclose(fMsg);
FILE* fMsg2 = fopen(f5Path,"w");
fprintf(fMsg2,"%s",msgcrypt);
fclose(fMsg2);

这是文件的内容位于 f4Path :

Salted__X¢~xÁïÈú™xe^„fl¯�˜<åD

现在是位于 f5Path 的文件内容:

Salted__X¢~xÁïÈú™xe^„fl¯

请注意,缺少 4 个字符。

有人有主意吗?

Using the C language, I am trying to manipulate some files generated by openssl and containing a lot of (very) special characters. But the end of file seems to be prematurely detected.
For example see an extract of my program, that is supposed to copy a file to another :

(for simplicity reasons I do not show the test of the opening of the file but I do that in my program)

char msgcrypt[FFILE];
FILE* fMsg = fopen(f4Path,"r");
while(fgets(tmp,FFILE,fMsg) != NULL) strcat(msgcrypt,tmp);
fclose(fMsg);
FILE* fMsg2 = fopen(f5Path,"w");
fprintf(fMsg2,"%s",msgcrypt);
fclose(fMsg2);

here is the content of the file located at f4Path :

Salted__X¢~xÁïÈú™xe^„fl¯�˜<åD

now the content of the file located at f5Path :

Salted__X¢~xÁïÈú™xe^„fl¯

Notice that 4 characters are missing.

Do someone have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2025-01-01 02:00:10

但是文件结尾似乎被过早检测到

听起来很熟悉。

  • 打开文件时使用fopen(f4Path, "rb")。这对于 Windows 来说具有真正的意义。
  • 不要使用字符串函数(fprintfstrcatfgets 等),它们会因 NUL 字符而阻塞。请改用 freadfwrite

But the end of file seems to be prematurely detected

Sounds familiar.

  • Use fopen(f4Path, "rb") when opening the file. This has real significance on Windows.
  • Don't use string functions (fprintf, strcat, fgets etc) they will choke on NUL characters. Use fread and fwrite instead.
灰色世界里的红玫瑰 2025-01-01 02:00:10

strcat 尝试复制一个以 null 结尾的 char *。这意味着,如果它遇到 0(它可能已经在这里完成了),它将停止复制。

你最好使用openreadmemcpywrite

strcat tries and copy a nul-terminated char *. Which means, if it encounters a 0, which it probably has done here, it will stop copying.

You'd better use open read, memcpy and write.

花开柳相依 2025-01-01 02:00:10

我把它停在的那个字符复制到十六进制编辑器中,如果我没记错的话,它最终会变成 EF BF BD,一个 BOM。因此,将文件作为文本文件读取失败。我没有看到任何 NULL 字符(除非复制和粘贴删除了它们)。

答案(正如已经讨论过的)是不要将其视为文本文件,并且避免使用 str 函数也不会造成任何损害。

我要做的第一件事是添加一个检查,检查字符的读取方式,这样您就知道数据在哪里被截断。现在它可以是以下任何一个:read、strcat、write。

That character it stops on I copied into a hex editor, and it ends up being EF BF BD, a BOM if I'm not mistaken. As a result, reading the file as a text file fails. I don't see any NULL characters (unless copying and pasting got rid of them).

The answer (as has already been discussed) is to not treat it as a text file, and avoiding the str functions won't do any harm either.

The first thing I'd do though is add a check for how may characters are read, that way you'll know where the data is being truncated. Right now it could be in any of: read, strcat, write.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文