All pastes #516933 Raw Edit

Mine

public text v1 · immutable
#516933 ·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()
{
   const char *end = "NONAME";
   char ime[32], prezime[128], jmbag[11], filename[25], *ptr;
   int bod[5], i, length = 0;
   FILE *fin;
   if ((fin = fopen("temp.log", "w+")) == NULL) {
      printf("Error opening temp file!\n");
      return 1;
   }
   while (1) {
      printf("Ime: ");
      fgets(ime, sizeof(ime), stdin);
      ime[strcspn(ime, "\n")] = 0x00;
      if (!strcmp(ime, end)) break;
      printf("Prezime: ");
      fgets(prezime, sizeof(prezime), stdin);
      prezime[strcspn(prezime, "\n")] = 0x00;
      printf("JMBAG: ");
      fgets(jmbag, sizeof(jmbag), stdin);
      jmbag[strcspn(jmbag, "\n")] = 0x00;
      for (i = 0; i < 4; i++) {
         do {
            printf("%d. bodovi: ", i + 1);
            scanf("%d", &bod[i]);
         } while (bod[i] < 0 || bod[i] > 20);
      }
      getchar();
      length += fprintf(fin, "%s#%s#%s#%d#%d#%d#%d#\n", 
         jmbag, ime, prezime, bod[0], bod[1], bod[2], bod[3]);
   }
   ptr = (char *) calloc(length + 1, 1);
   i = 0;
   rewind(fin);
   fread(ptr, 1, length, fin);
   fclose(fin);
   printf("Filename: ");
   fgets(filename, sizeof(filename), stdin);
   filename[strcspn(filename, "\n")] = 0x00;
   if (write_file(ptr, filename)) printf("Error\n");
   else printf("Sucessfull!\n");
   free(ptr);
   getche();
   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;
}