文件操作的程序,帮我看看
这个程序想实现从标准输入输入一行,就写入文件一行,直到输入quit为止,可是为什么程序编译通过,运行也不打印错误,但是就是写不进文件呢?而且还有奇怪的回显,你输入一行,它回显一行,加个奇怪的小尾巴.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
#define SIZE 10
int main(void)
{
int outfile;
char filename[] = "test.dat";
char buffer[SIZE];
if(outfile=open(filename, NEWFILE, 0640)==-1)
{
printf("Error, Open file failed!\n");
exit(255);
}
gets(buffer);
while(strcmp(buffer,"quit"))
{
if(write(outfile, buffer, sizeof(buffer))==-1)
{
printf("ERROR,WRITE FAILED: \n",sys_errlist[errno]);
exit(255);
}
gets(buffer);
}
close(outfile);
return 0;
}