write(_fd, buffer, MAXCHAR); i = 0; while(j < MAXCHAR)
write(_fd, buffer, FORTUNEMAXCHAR); i = 0; while(j < FORTUNEMAXCHAR)
int txt2bin(char *file){     int i = 0, j = 0, fd = 0, _fd = 0, s = 1, c = 0, t = 0;     char b, buffer[MAXCHAR];     char lastchar, temp;          fd = open(file, 0);     sprintf(buffer, "%s.dat", file);     _fd = creat(buffer, 0777);          if(fd == -1) {               printf("No such file!");               return 0;     }          //First, find number of entries     //Never use % ANYWHERE     #ifndef HEADER     s = 1;          while(s > 0) {             while(s = read(fd, &b, 1) > 0) {                     temp = b;                     if(b == '\n')                            b = ' ';                     if((b == ' ' && lastchar == ' ') || b >= 127 || (b <= 31 && (b != 10 || b != '\n')))                     {                          b = 0;                          continue;                     }                     //find \n%                     if(b == '%' && lastchar == '\n')                      {                           //Write and reset                           c++;                           i = 0;                           continue;                     }                     else if(i >= MAXCHAR)                     {                          i = 0;                          //Ok, get to \n%                          while(s = read(fd, &b, 1) > 0 && (b != '%' || lastchar != '\n')) {                                  lastchar = b;                                  b = 0;                          }                          b = 0;                          continue;                     }                     lastchar = temp;                     i++; //Char count                     b = 0;             }     }     printf("Fortune Cookies Generated: %d\n", c);          //Rewind     lseek(fd, 0L, 0);          //Write number of entries. Count on + 0, thus % after the last too     write(_fd, &c, sizeof(c));          //Make sure the buffer is initilaised     while(j < MAXCHAR)             buffer[j++]='\0';     j = 0;     s = 1;     lastchar = 0;          while(s > 0) {             while(s = read(fd, &b, 1) > 0) {                     temp = b;                     if(b == '\n')                            b = ' ';                     if((b == ' ' && lastchar == ' ') || b >= 127 || (b <= 31 && (b != 10 || b != '\n')))                     {                          b = 0;                          continue;                     }                     //find \n%                     if(b == '%' && lastchar == '\n')                      {                           //Write and reset                           write(_fd, buffer, MAXCHAR);                           i = 0;                           while(j < MAXCHAR)                                   buffer[j++]='\0';                           j = 0;                           continue;                     }                     else if(i >= MAXCHAR)                     {                          i = 0;                          while(j < MAXCHAR)                                   buffer[j++]='\0';                          j = 0;                          //Ok, get to \n%                          while((s = read(fd, &b, 1) > 0) && (b != '%' || lastchar != '\n')) {                                  lastchar = b;                                  b = 0;                          }                          b = 0;                          continue;                     }                     lastchar = temp;                                          buffer[i++] = b;                     b = 0;             }     }     #else     j = 0;     while(j < MAXCHAR)             buffer[j++]='\0';     //Skip the header     write(fd, &c, (long)sizeof(c));     //This takes almost as long as copying, for accuracy purposes     while(s > 0) {             while(s = read(fd, &b, 1) > 0) {                     if(b == '\n')                            b = ' ';                     if((b == ' ' && buffer[i-1] == ' ') || b >= 127 || (b <= 31 && (b != 10 || b != '\n')))                     {                          b = 0;                          continue;                     }                     if(b == '%')                      {                           //Note and reset                           c++;                           t += (i+1);                           write(_fd, &t, sizeof(t));                           i = 0;                           while(j < MAXCHAR)                                   buffer[j++]='\0';                           j = 0;                           continue;                     }                     else if(i >= MAXCHAR)                     {                          i = 0;                          b = 0;                          while(j < MAXCHAR)                                   buffer[j++]='\0';                          j = 0;                          continue;                     }                     buffer[i++] = b;                     b = 0;             }     }     //Reset buffer     j = 0;     while(j < MAXCHAR)             buffer[j++]='\0';     //Write c     lseek(_fd, 0L, 0);     write(_fd, &c, sizeof(c));     lseek(_fd, (long)(sizeof(t) * c), 0);     lseek(fd, 0L, 0);     //Now...copy it     j = 0;     s = 1;          while(s > 0) {             while(s = read(fd, &b, 1) > 0) {                     if(b == '\n')                            b = ' ';                     if((b == ' ' && buffer[i-1] == ' ') || b >= 127 || (b <= 31 && (b != 10 || b != '\n')))                     {                          b = 0;                          continue;                     }                     if(b == '%' && buffer[i-1] == '\n')                      {                           //Write and reset                           buffer[i+1]='\0';                           write(_fd, buffer, i+1);                           i = 0;                           while(j < MAXCHAR)                                   buffer[j++]='\0';                           j = 0;                           continue;                     }                     else if(i >= MAXCHAR)                     {                          i = 0;                          b = 0;                          while(j < MAXCHAR)                                   buffer[j++]='\0';                          j = 0;                          continue;                     }                     buffer[i++] = b;                     b = 0;             }     }     #endif}