Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.
| Obě strany předchozí revizePředchozí verze | |||
| pitel:ijc:du2 [08. 04. 2013, 09.21:56] – file pitel | pitel:ijc:du2 [30. 12. 2022, 13.43:01] (aktuální) – upraveno mimo DokuWiki 127.0.0.1 | ||
|---|---|---|---|
| Řádek 1: | Řádek 1: | ||
| + | ====== Domácí úkol č.2 ====== | ||
| + | <file make Makefile> | ||
| + | CC = gcc | ||
| + | CFLAGS = -std=c99 -pedantic -Wall -O3 | ||
| + | PROGS = tail | ||
| + | LOGIN = xkalab00 | ||
| + | ARCHIVER = zip | ||
| + | all: $(PROGS) | ||
| + | |||
| + | tail: tail.c | ||
| + | |||
| + | clean: | ||
| + | -rm -f $(PROGS) | ||
| + | |||
| + | pack: | ||
| + | -$(ARCHIVER) $(LOGIN).zip *.c Makefile | ||
| + | </ | ||
| + | <file c tail.c> | ||
| + | // tail.c | ||
| + | // Řešení IJC-DU2, příklad 1a) | ||
| + | // Autor: Jan Kaláb, FIT | ||
| + | // Přeloženo gcc 4.1.1 || 4.1.2 | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | FILE *otevri(char *soubor) { | ||
| + | // | ||
| + | FILE *fp = fopen(soubor, | ||
| + | return fp; | ||
| + | } | ||
| + | |||
| + | int seekprint(char *pozadovany_soubor, | ||
| + | FILE *soubor = otevri(pozadovany_soubor); | ||
| + | if (soubor == NULL) { //kdyz se to nepovedlo | ||
| + | fprintf(stderr, | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | |||
| + | //Jdem hledat poslednich n radku | ||
| + | fseek(soubor, | ||
| + | long delka = ftell(soubor); | ||
| + | fseek(soubor, | ||
| + | // | ||
| + | long offset = 0; | ||
| + | unsigned int prectenych_radku = 0; | ||
| + | if (odzacatku != 0) prectenych_radku = 2; //todle moc nevim proc zrovna 2 | ||
| + | int znak; | ||
| + | while (prectenych_radku <= pozadovanych_radku) { | ||
| + | if (odzacatku != 0) fseek(soubor, | ||
| + | if (abs(offset) > delka) { //kdyz uz jsme moc daleko | ||
| + | // | ||
| + | if (odzacatku != 0) fseek(soubor, | ||
| + | break; | ||
| + | } | ||
| + | znak = getc(soubor); | ||
| + | if (znak == ' | ||
| + | if (odzacatku != 0) offset++; else offset--; | ||
| + | } | ||
| + | // | ||
| + | // | ||
| + | |||
| + | //Vyblejem to | ||
| + | while ((znak = getc(soubor)) != EOF) putchar(znak); | ||
| + | |||
| + | //Skoncime | ||
| + | fclose(soubor); | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | |||
| + | int main(int argc, char *argv[]) { | ||
| + | if (argc == 1) { //stdin | ||
| + | //malloc | ||
| + | unsigned int alok = 1; | ||
| + | int *text = malloc(alok * sizeof(int)); | ||
| + | if (text == NULL) { | ||
| + | fprintf(stderr, | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | |||
| + | //cteni | ||
| + | int znak = getchar(); | ||
| + | unsigned int precteno=1; | ||
| + | |||
| + | while (znak != EOF) { | ||
| + | // | ||
| + | if (precteno >= alok) {//realloc | ||
| + | alok*=2; | ||
| + | if ((text = realloc(text, | ||
| + | fprintf(stderr, | ||
| + | free(text); | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | } | ||
| + | text[precteno-1] = znak; | ||
| + | znak = getchar(); | ||
| + | precteno++; | ||
| + | } | ||
| + | |||
| + | //najdem poslednich 10 radku | ||
| + | unsigned int vypis=precteno; | ||
| + | unsigned int radku=0; | ||
| + | while (radku <= 10 && vypis > 0) { | ||
| + | vypis--; | ||
| + | if (text[vypis-1] == ' | ||
| + | } | ||
| + | |||
| + | // | ||
| + | while (vypis < precteno) { | ||
| + | putchar(text[vypis]); | ||
| + | vypis++; | ||
| + | } | ||
| + | // | ||
| + | |||
| + | // | ||
| + | free(text); | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | if (argc == 2) { //10 radku | ||
| + | if (seekprint(argv[1], | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | if (argc == 3) { //spesl | ||
| + | unsigned int pozadovanych_radku = atoi(argv[1]+1); | ||
| + | // | ||
| + | if (argv[1][0] != ' | ||
| + | fprintf(stderr, | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | if (argv[1][0] == ' | ||
| + | if (seekprint(argv[2], | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | if (argv[1][0] == ' | ||
| + | if (seekprint(argv[2], | ||
| + | return EXIT_SUCCESS; | ||
| + | } | ||
| + | } | ||
| + | fprintf(stderr, | ||
| + | return EXIT_FAILURE; | ||
| + | } | ||
| + | </ | ||