Reputation: 305
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.
Upvotes: 2
Views: 414
Reputation: 8537
You have a buffer overflow error in your code. You declare redak
as:
char redak[6];
And then overwrite memory past the end of it with this line:
redak[6] = '\0';
The result is an undefined behavior. Note that tools like valgrind
would help to catch this kind of errors easily.
Another problem is with your for
loop: the condition should be j<6
instead of 7.
Finally, redak
should be of size 7 and the fgets()
changed accordingly. Otherwise fgets
does not read the newline character in, and so you start reading the "next" line from the wrong position. Remember that fgets
reads maximally one less than size characters.
This all also means that char znak[5][5 + 1]
should be changed to char znak[6][7]
.
Upvotes: 1