0
我有一个简单的打开和写入奇怪的结果。我会引用该程序,然后我会解释我的结果:写在文件中产生奇怪的结果
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(){
int fd = open("hello.txt", O_WRONLY|O_CREAT, 0700);
char buf[] = "Hello world\n";
int i=0;
for(i = 0; i < 10;i++){
write(1,buf,sizeof(buf));
write(fd, buf, sizeof(buf));
}
close(fd);
return 0;
}
使用此代码,在终端输出将是世界,你好十倍预期......但在文件hello.txt的我得到这个:
效汬潷汲䠀汥潬眠牯摬
效汬潷汲䠀汥潬眠牯摬
效汬潷汲䠀汥潬眠牯摬
效汬潷汲䠀汥潬眠牯摬
效汬潷汲䠀汥潬眠牯摬
1)为什么会发生这种情况?我做错了什么?为什么是中国人? 在此先感谢
编辑:使用gcc 4.8.1与-Wall标志进行编译:没有警告
我编译并运行它时没有遇到这个错误。 – squiguy
适合我。你可能想检查你的终端设置。 – Asblarf
你打电话给'open'后检查过fd!= -1吗? –