All pastes #516938 Raw Edit

Anonymous

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