/* Write C program which in main function loads a text from keyboard. Text is unlimited length, it can have more lines and it ends with word 'END'. After END, program asks user for filename. Function opens a file and writes entered text inside with following rules. Every sentance has to be begin in new line, sentences finishes with one of the characters (.!?). Word END is not written to the file. Example (input from keyboard): First sentense is this.What is it ?Today is a lovely day, but tomorrow will rain.Now is enough ! Example (output file): First sentense is this. What is it ? Today is a lovely day, but tomorrow will rain. Now is enough ! */ #include int write_file(char *text, char *file); int main() { FILE *fin; int i, ch, end = 0, length = 0; char *end_str = "END", filename[25], *p; fin = fopen("temp.log", "w+"); while ((ch = getchar()) != EOF) { if (ch != 'E' && ch != 'N' && ch != 'D') { for (i = 0; i < end; i++) fputc(end_str[i], fin); end = 0; length++; fputc(ch, fin); if (ch == '.' || ch == '!' || ch == '?') { fputc('\n', fin); length++; } } else { if (!end && (ch == 'N' || ch == 'D')) { fputc(ch, fin); length++; } else { if (end_str[end] == ch) { end++; if (end == 3) break; } } } } i = 0; p = (char *) calloc(length, sizeof(char)); rewind(fin); while ((ch = fgetc(fin)) != EOF) p[i++] = ch; p[length] = 0x00; fclose(fin); printf("Naziv datoteke: ", length); scanf("%s", filename); if (zapisi(p, filename)) { printf("Problemi pri kreiranju datoteke!\n"); } else { printf("Datoteka je uspjesno kreirana\n"); } getche(); free(p); return 0; } int write_file(char *text, char *file) { FILE *fin; if ((fin = fopen(file, "w")) == NULL) return 1; if (fwrite(text, sizeof(char), strlen(text), fin) < strlen(text)) { fclose(fin); return 1; } fclose(fin); return 0; }