All pastes #516934 Raw Edit

Stuff

public text v1 · immutable
#516934 ·published 2007-05-28 22:10 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int write_file(const char *text, const char *file);
int main()
{
   FILE *fin;
   int i, ch, end = 0, length = 0;
   const char *end_str = "END", *end_sen = ".!?";
   char filename[25], *p;
   if ((fin = fopen("temp.log", "w+")) == NULL) {
      printf("Error opening file\n");
      return 1;
   }
   while (end != 3) {
      ch = getchar();
      if (!strchr(end_str, ch)) {
         for (i = 0; i < end; i++) fputc(end_str[i], fin);
         end = 0;
         fputc(ch, fin);         
         if (strchr(end_sen, ch)) fputc('\n', fin);
      } else if (!end && strchr(end_str + 1, ch)) {
         fputc(ch, fin);
      } else if (end_str[end] == ch)
         end++;
   }
   i = 0;
   length = (int) ftell(fin);
   p = (char *) calloc(length + 1, 1);
   rewind(fin);
   fread(p, 1, length, fin);
   fclose(fin);
   getchar();
   printf("Filename: ");
   fgets(filename, sizeof(filename), stdin);
   filename[strcspn(filename, "\n")] = 0x00;
   if (write_file(p, filename))
      printf("Error\n");
   else
      printf("Sucessfull!\n");
   getche();
   free(p);
   return 0;
}
int write_file(const char *text, const char *file)
{
   FILE *fin;
   if ((fin = fopen(file, "w")) == NULL)
      return 1;
   if (fwrite(text, 1, strlen(text), fin) < strlen(text)) {
      fclose(fin);
      return 1;
   }
   fclose(fin);
   return 0;
}