I am writing a program that takes an input file that consists of drawings of characters made up of hashtags (A, B, C...), reads a string from a console and prints out that string by drawing the characters according to the input file.
Note: As you will see below, the input file contains '.' instead of spaces. These are replaced with spaces in my code.
For some reason when I print a newline after a character is drawn, pieces of the character disappear.
I know it sounds complicated so I will provide all the file and the output below.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct{
char znak[5][5 + 1];
} slovo;
void napuni(slovo *znakovi, FILE *ulaz);
int main(){
int i,j;
FILE *ulaz = fopen("ascii.in", "r");
slovo *znakovi = calloc(27, sizeof(slovo));
napuni(znakovi, ulaz);
fclose(ulaz);
FILE *izlaz = fopen("ascii.out", "w");
char *tekst = calloc(50,sizeof(char));
fgets(tekst,50,stdin);
char *znak = strchr(tekst, '#');
*znak = '\0';
int duljina = strlen(tekst);
for (j = 0; j < 5; j++){
for (i = 0; i < duljina; i++){
int znak2 = toupper(tekst[i]) - 65;
fprintf(izlaz, "%s", znakovi[znak2].znak[j]);
}
fprintf(izlaz,"\n");
}
fclose(izlaz);
return 0;
}
void napuni(slovo *znakovi, FILE *ulaz){
char redak[6];
int i,j;
for (i = 0; i < 27; i++){
for (j = 0; j < 7; j++){
if (feof(ulaz)) break;
fgets(redak,6,ulaz);
if (redak[0] == '\n' || redak[0] == 32) continue;
char *znak = strchr(redak, '.');
while(znak != NULL){
*znak = 32;
znak = strchr(redak,'.');
}
strcpy(znakovi[i].znak[j], redak);
printf("\n%s",znakovi[i].znak[j]); // this line prints the character to console for testing
}
printf("\n"); //when this line is uncommented pieces of the character disappear
}
}
Here is the input file:
..#..
.#.#.
.###.
.#.#.
.#.#.
.##..
.#.#.
.##..
.#.#.
.##..
..#..
.#.#.
.#...
.#.#.
..#..
.##..
.#.#.
.#.#.
.#.#.
.##..
.###.
.#...
.##..
.#...
.###.
.###.
.#...
.##..
.#...
.#...
..#..
.#...
.#.##
.#..#
..##.
.#.#.
.#.#.
.###.
.#.#.
.#.#.
..#..
..#..
..#..
..#..
..#..
...#.
...#.
...#.
.#.#.
..#..
.#.#.
.#.#.
.##..
.#.#.
.#.#.
.#...
.#...
.#...
.#...
.###.
#...#
##.##
#.#.#
#...#
#...#
#..#.
##.#.
#.##.
#..#.
#..#.
..#..
.#.#.
.#.#.
.#.#.
..#..
.##..
.#.#.
.##..
.#...
.#...
..#..
.#.#.
.#.#.
.#.#.
..##.
.##..
.#.#.
.##..
.##..
.#.#.
..##.
.#...
.###.
...#.
.##..
.###.
..#..
..#..
..#..
..#..
.#.#.
.#.#.
.#.#.
.#.#.
.###.
.#.#.
.#.#.
.#.#.
.#.#.
..#..
#...#
#...#
#.#.#
#.#.#
.#.#.
#...#
.#.#.
..#..
.#.#.
#...#
.#.#.
.#.#.
.###.
...#.
..##.
.###.
...#.
..#..
.#...
.###.
..#..
.#.#.
...#.
..#..
..#..
Here is a test output (just printing out each character one at a time) when I don't insert a newline character in between:
#
# #
###
# #
# #
##
# #
##
# #
##
#
# #
#
# #
#
##
# #
# #
# #
##
###
#
##
#
###
###
#
##
#
#
#
#
# ##
# #
##
# #
# #
###
# #
# #
#
#
#
#
#
#
#
#
# #
#
# #
# #
##
# #
# #
#
#
#
#
###
# #
## ##
# # #
# #
# #
# #
## #
# ##
# #
# #
#
# #
# #
# #
#
##
# #
##
#
#
#
# #
# #
# #
##
##
As you can see it behaves as expected. However if I insert a newline character in between, the characters get mangled up. See below:
#
# #
###
# #
# #
##
# #
##
# #
##
#
# #
#
# #
#
##
# #
# #
# #
##
###
#
##
#
###
###
#
##
#
#
#
#
# ##
# #
##
# #
# #
###
# #
# #
#
#
#
#
#
#
#
#
# #
#
# #
# #
##
# #
# #
#
#
#
#
###
# #
## ##
# # #
# #
# #
# #
## #
# ##
# #
# #
#
# #
# #
# #
#
##
# #
##
#
#
#
# #
# #
# #
##
##
That's basically it. I am a beginner in C so any help is appreciated.
redak[6] = '\0';invokes UB. Remove it asfgetsdoes the job automatically.if (redak[0] == '\n' || redak[0] == 32) continue;→if (redak[0] == '\n' || redak[0] == 32) {j--; continue;}for (j = 0; j < 5; j++){ for (i = 0; i < duljina; i++){tofor (i = 0; i < duljina; i++){ for (j = 0; j < 5; j++){