|
想要创建一test。dat的文件,并从终端输入若干字符串写入文件,直至输入字符串“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 BUFSIZE 1024
#define SIZE 80
int write_buffer(int fd, const void *buf, int count);
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);
}
while(!strcmp(buffer, "quit"))
{
gets(buffer);
if (write_buffer(outfile,buffer,SIZE)==-1)
{
printf("ERROR,WRITE FAILED:\n", sys_errlist[errno]);
exit(255);
}
}
close(outfile);
return 0;
}
int my_write(int fd, char *buf, size_t count)
{
int i,n,write_offset; write_offset=0;
for(i=0;i<count;++i)
{
write_buffer[write_offset++]=*buf++;
if (write_offset==BUFSIZE)
{
write_offset=0;
n= write( fd,write_buffer,sizeof(write_buffer));
if (n!=BUFSIZE)
return -1;
}
}
return -1;
}
提示:
test1.c:10:26: macro parameters must be comma-separated
test1.c: In function `main':
test1.c:23: error: `NEWFILE' undeclared (first use in this function)
test1.c:23: error: (Each undeclared identifier is reported only once
test1.c:23: error: for each function it appears in.)
test1.c:23: error: invalid lvalue in assignment
test1.c: In function `my_write':
test1.c:47: error: subscripted value is neither array nor pointer |
|