LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 605|回复: 1

帮忙看看

[复制链接]
发表于 2004-11-7 16:24:31 | 显示全部楼层 |阅读模式
想要创建一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
发表于 2004-11-7 22:00:15 | 显示全部楼层
#define NEWFILE(O_WRONLY O_CREAT O_TRUNC)
NEWFILE后面应该有个空格
if(outfile=open(filename,NEWFILE,0640)=-1)
outfile=open(filename,NEWFILE,0640)外面应该有括号
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表