school/stringyreloaded.c
2024-06-10 15:57:26 +02:00

366 lines
8.0 KiB
C

//
// Created by bruno on 3/18/24.
//
#include <unistd.h>
#include "stringyreloaded.h"
#include "curses.h"
#include "string.h"
int stringyreloaded() {
char meno[64];
char adresa[64];
char telefon[10];
char *s1 = "Ahoj";
char s2[21];
FILE *fw;
fw = fopen("test.txt", "w");
strcpy(s2, "Hello");
strcat(s2, " + ");
strcat(s2, s1);
fprintf(fw, "Dĺžka reťazca %s je %lu.\n", s1, strlen(s1));
fprintf(fw, "Dĺžka reťazca %s je %lu.\n", s2, strlen(s2));
initscr();
clear();
puts("Zadaj meno a priezvisko: ");
getnstr(meno, 63);
puts("Zadaj adresu: ");
getnstr(adresa, 63);
puts("Zadaj telefón: ");
getnstr(telefon, 9);
fputs("Zadal si:\n", fw);
fputs(meno, fw);
fputc('\n', fw);
fputs(adresa, fw);
fputc('\n', fw);
fputs(telefon, fw);
fputc('\n', fw);
fclose(fw);
return 0;
}
int riadky() {
FILE *fp;
char stuff[25];
fp = fopen("s.txt", "w");
strcpy(stuff, "Toto je riadok");
for (int index = 1; index <= 10; index++) {
fprintf(fp, "%s, riadok číslo %d.\n", stuff, index);
}
fclose(fp);
}
int pridavanie() {
FILE *fp;
char stuff[35];
fp = fopen("s.txt", "a");
strcpy(stuff, "Toto je dalsi");
for (int index = 0; stuff[index]; index++) {
putc(stuff[index], fp);
putc('\n', fp);
}
fclose(fp);
}
int citanie() {
FILE *fp;
char c;
fp = fopen("s.txt", "r");
if (fp) {
do {
c = getc(fp);
if (c != EOF) {
putchar(c);
}
} while (c != EOF);
} else {
printf("s.txt neexistuje.");
}
return 0;
}
int cislovanie() {
FILE *fp;
char c;
int pocet = 1;
fp = fopen("s.txt", "r");
if (fp) {
printf("%d: ", pocet);
do {
c = getc(fp);
if (c == '\n') {
c = getc(fp);
if (!feof(fp)) {
printf("\n%d: ", ++pocet);
}
ungetc(c, fp);
} else {
if (!feof(fp)) {
putchar(c);
}
}
} while (!feof(fp));
fclose(fp);
} else {
printf("s.txt neexistuje.");
}
printf("Pozrite si súbor pismena2.txt");
getch();
return 0;
}
int naVelke() {
FILE *fr, *fw;
int c;
if ((fw = fopen("pismena2.txt", "w")) == NULL) {
printf("pismena2.txt nejde otvoriť.");
return 1;
}
if ((fr = fopen("pismena.txt", "r")) == NULL) {
printf("pismena.txt nejde otvoriť.");
return 1;
} else {
while ((c = getc(fr)) != EOF) {
if (c >= 'a' && c <= 'z') {
putc(c + 'A' - 'a', fw);
} else {
putc(c, fw);
}
}
}
if (fclose(fw) == EOF) {
printf("Súbor pismena2.txt sa nezavrel.");
}
if (fclose(fr) == EOF) {
printf("Súbor pismena.txt sa nezavrel.");
}
printf("Pozrite si pismena2.txt");
getch();
return 0;
}
int kopirovanie(int argc, char *argv[]) {
int c;
if (argc >= 3) {
FILE *fr, *fw;
if ((fr = fopen(argv[1], "r")) == NULL) {
printf("Súbor %s nejde otvoriť.", argv[1]);
return 1;
}
if ((fw = fopen(argv[2], "w")) == NULL) {
printf("Súbor %s nejde otvoriť.", argv[2]);
return 1;
}
while ((c = getc(fr)) != EOF) {
putc(c, fw);
}
if (fclose(fw) == EOF) {
printf("Súbor %s sa nezavrel.", argv[2]);
}
if (fclose(fr) == EOF) {
printf("Súbor %s sa nezavrel.", argv[1]);
}
printf("Skopírované, pozrite si %s", argv[2]);
getch();
return 0;
} else {
printf("%s subor1 subor2", argv[0]);
return 1;
}
}
int redirector() {
FILE *fw;
int c;
printf("Stlacte O pre vypis na obrazovku, \n"
"alebo iny znak pre zapis do suboru vystup.txt: ");
c = getchar();
while (getchar() != '\n');
if (c == 'o' || c == 'O') {
fw = stdout;
} else {
if (access("vystup.txt", F_OK) == 0) {
printf("Súbor vystup.txt už existuje, chcete ho prepísať? [A/N]");
c = getchar();
while (getchar() != '\n');
if (!(c == 'a' || c == 'A')) {
return 0;
}
}
if ((fw = fopen("vystup.txt", "w")) == NULL) {
printf("Súbor vystup.txt sa nepodarilo otvoriť.");
return 1;
}
}
printf("Píšte niečo, ukončite hviezdičkou:");
while ((c = getchar()) != '*') {
putc(c, fw);
}
if (fw != stdout) {
printf("Pozrite sa do vystup.txt");
if (fclose(fw) == EOF) {
printf("Súbor vystup.txt sa nepodarilo zatvoriť.");
return 1;
}
}
return 0;
}
int cislovanie2() {
FILE *fp;
char buf[25];
int index;
fp = fopen("s.txt", "w");
if (fp == NULL) {
printf("Chyba pri otváraní súboru");
return 1;
}
strcpy(buf, "Toto je riadok.");
for (index = 1; index <= 10; index++) {
fprintf(fp, "%s Číslo riadku %d\n", buf, index);
}
if (fclose(fp) == EOF) {
printf("Chyba pri zatváraní súboru.");
}
return 0;
}
int appendovanie() {
FILE *fp;
char buf[35];
int index, count;
fp = fopen("s.txt", "a");
if (fp == NULL) {
printf("Chyba pri otváraní súboru");
return 1;
}
strcpy(buf, "Toto je riadok.");
for (count = 1; count <= 5; count++) {
for (index = 0; buf[index]; index++) {
putc(buf[index], fp);
putc('\n', fp);
}
}
if (fclose(fp) == EOF) {
printf("Chyba pri zatváraní súboru.");
}
return 0;
}
int citanieSuboru() {
FILE *fp;
char c;
fp = fopen("s.txt", "r");
if (fp == NULL) {
printf("Chyba pri otváraní súboru");
return 1;
}
do {
c = getc(fp);
putchar(c);
} while (c != EOF);
if (fclose(fp) == EOF) {
printf("Chyba pri zatváraní súboru.");
}
return 0;
}
int citanieSuboru2() {
FILE *fp;
char c;
int pocet = 1;
fp = fopen("s.txt", "r");
if (fp == NULL) {
printf("Chyba pri otváraní súboru");
return 1;
}
printf("\n%d: ", pocet);
do {
c = getc(fp);
if (c == '\n') {
if (feof(fp) == 0) {
c = getc(fp);
printf("\n%d: ", ++pocet);
ungetc(c, fp);
}
} else {
putchar(c);
}
} while (c != EOF);
if (fclose(fp) == EOF) {
printf("Chyba pri zatváraní súboru.");
}
return 0;
}
int poSlovach() {
FILE *fp;
char slovo[100];
char c;
fp = fopen("s.txt", "r");
if (fp == NULL) {
printf("Nepodarilo sa otvoriť s.txt");
return 1;
}
do {
c = fscanf(fp, "%s", slovo);
printf("%s\n", slovo);
} while (c != EOF);
fclose(fp);
return 0;
}
int poRiadkoch() {
FILE *fp;
char slovo[100];
char *c;
fp = fopen("s.txt", "r");
if (fp == NULL) {
printf("Nepodarilo sa otvoriť s.txt");
return 1;
}
do {
c = fgets(slovo, 100, fp);
printf("%s\n", slovo);
} while (c != NULL);
fclose(fp);
return 0;
}
int input(int argc, char *argv[]) {
FILE *fp;
char slovo[100];
char *c;
if (argc == 1) {
printf("Použitie: %s filename\n", argv[0]);
} else {
if (argc > 2) {
printf("Spracuje sa len 1. parameter");
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Nepodarilo sa otvoriť %s", argv[1]);
return 1;
}
do {
c = fgets(slovo, 100, fp);
if (c != NULL) {
printf("%s\n", slovo);
}
} while (c != NULL);
fclose(fp);
}
return 0;
}