C中特殊字符文件的读写
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来很熟悉。
fopen(f4Path, "rb")
。这对于 Windows 来说具有真正的意义。fprintf
、strcat
、fgets
等),它们会因NUL
字符而阻塞。请改用fread
和fwrite
。Sounds familiar.
fopen(f4Path, "rb")
when opening the file. This has real significance on Windows.fprintf
,strcat
,fgets
etc) they will choke onNUL
characters. Usefread
andfwrite
instead.strcat
尝试复制一个以 null 结尾的char *
。这意味着,如果它遇到 0(它可能已经在这里完成了),它将停止复制。你最好使用
open
、read
、memcpy
和write
。strcat
tries and copy a nul-terminatedchar *
. Which means, if it encounters a 0, which it probably has done here, it will stop copying.You'd better use
open
read
,memcpy
andwrite
.我把它停在的那个字符复制到十六进制编辑器中,如果我没记错的话,它最终会变成 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.