- 论坛徽章:
- 0
|
写如下测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define STD_INPUT 0
#define STD_OUTPUT 1
#define STD_ERROR 2
#define WRITE_TEST
int main(int argc,char* argv[])
{
writeFile();
exit(0);
}
#ifdef WRITE_TEST
int writeFile()
{
FILE *fp;
int fd;
char buffer[15];
//unlink("text.txt");
fd = open("text.txt",O_RDWR|O_CREAT,S_IWUSR);
if(fd == -1)
printf("create file fail!\n");
if(write(fd,"write text!\n",15) != 15)
{
write(STD_ERROR,"a write error happend!\n",sizeof("a write error happend!\n"));
}
read(fd,buffer,15);
write(STD_OUTPUT,buffer,15);
return 1;
}
#endif
最后执行,输出为乱码。不知原因为何,请高手解惑。 |
|