Last active
January 20, 2018 19:32
-
-
Save darkodemic/3821a4b6805a03b63e14f0d530811f70 to your computer and use it in GitHub Desktop.
ProgramskiJezici_Jan_2016_Grupa1_zadatak1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
/* | |
Задатак 1. | |
Написати програм на језику С који: | |
а) Чита са тастатуре имена три датотеке: једне изворне и две одредишне; | |
b) Отвара као текстуалне, прву датотеку за читање, другу и трећу за упис и читање; | |
c) Копира садржај изворне текст датотеке: у прву одредишну датотеку тако што сваки | |
празан знак замењује знаком доња црта ( _ ), а у другу одредишну датотеку тако што | |
свако слово после празног знака мења у исто велико слово. | |
d) Приказује на екрану садржаје свих датотека на следећи начин: 1. ред из сваке од | |
датотека, 2. ред из све од датотека,...,последњи ред из сваке од датотека. | |
*/ | |
// CLI args | |
// 0 1 2 3 | |
// progname, dat1, dat2, dat3 | |
void checkFileStatus(FILE *fp) { | |
if (fp == NULL) { | |
fprintf(stderr, "File could not be opened."); | |
exit(1); | |
} | |
} | |
void buildFirstFile(FILE *fpSource, FILE *fpOutput1) { | |
char c; | |
while ((c = fgetc(fpSource)) != EOF) { | |
if (c == ' ') { | |
fputc('_', fpOutput1); | |
} | |
else { | |
fputc(c, fpOutput1); | |
} | |
} | |
} | |
void buildSecondFile(FILE *fpSource, FILE *fpOutput2) { | |
char c; | |
int spaceFlag = 0; | |
rewind(fpSource); | |
while ((c = fgetc(fpSource)) != EOF) { | |
if (c == ' ') { | |
fputc(c, fpOutput2); | |
if (spaceFlag == 0) { | |
spaceFlag = 1; | |
} | |
} | |
else if (spaceFlag == 1 && c != ' ') { | |
if (isalpha(c)) { | |
fputc(toupper(c), fpOutput2); | |
} | |
else { | |
fputc(c, fpOutput2); | |
} | |
spaceFlag = 0; | |
} | |
else { | |
fputc(c, fpOutput2); | |
} | |
} | |
} | |
char outputLine(FILE *fp) { | |
char c; | |
while ((c = fgetc(fp)) != '\n') { | |
if (c == EOF) { | |
return 'e'; // e for exit. Why? Cuz I like it like that. Yes, yes I do. Maybe, or not. Anyway..... | |
} | |
printf("%c", c); | |
} | |
} | |
void outputAllDataFiles(FILE *fpSource, FILE *fpOutput1, FILE *fpOutput2) { | |
rewind(fpSource); | |
rewind(fpOutput1); | |
rewind(fpOutput2); | |
while(1) { | |
printf("\n"); | |
if (outputLine(fpSource) == 'e') { | |
break; | |
}; | |
printf("\n"); | |
outputLine(fpOutput1); | |
printf("\n"); | |
outputLine(fpOutput2); | |
printf("\n-------------------------------------------"); | |
}; | |
} | |
int main(int argc, char *argv[]) { | |
if (argc <= 3) { | |
fprintf(stderr, "Not all required files are provided. 3 File names required. Exiting."); | |
exit(1); | |
} | |
FILE *fpSource = fopen(argv[1], "r"); | |
if (fpSource == NULL) { | |
fprintf(stderr, "File could not be opened."); | |
exit(1); | |
} | |
FILE *fpOutput1 = fopen(argv[2], "w+"); | |
if (fpOutput1 == NULL) { | |
fprintf(stderr, "File could not be opened."); | |
exit(1); | |
} | |
FILE *fpOutput2 = fopen(argv[3], "w+"); | |
if (fpOutput2 == NULL) { | |
fprintf(stderr, "File could not be opened."); | |
exit(1); | |
} | |
buildFirstFile(fpSource, fpOutput1); | |
buildSecondFile(fpSource, fpOutput2); | |
outputAllDataFiles(fpSource, fpOutput1, fpOutput2); | |
fclose(fpSource); | |
fclose(fpOutput1); | |
fclose(fpOutput2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment