I would handle your file as binary. I do not know which OS,API you are using so I will answer just in generic way...
get file size siz
usually seeking to 0
bytes from end of file will give you the file size.
allocate 1D array dat
to store your entire file
dat = new BYTE[siz];
load your file into memory (1D array)
do not forget to use binary access as some ASCII arts could use control codes (ASCII below 32) which could be corrupted by text file access.
scan for end of line
so scan your array from 0 and stop when you find ASCII codes 13
or 10
. Its position will give you the x
resolution of your ASCII art
int xs;
for (xs=0;xs<siz;xs++)
if ((dat[xs]==10)||(dat[xs]==13))
break;
now xs
should be holding your x
resolution.
compute y resolution ys
the safest way is to count the number of end of lines (13 or 10). In such case you can even store the line start addresses to some pointer array BYTE **pixel=new (BYTE*)[ys];
which will enable you simple 2D access pixel[y][x]
. If your ASCII art is aligned and have constant size per each line than you can compute ys
from size..
ys = siz/(xs+eol_size)
where eol_size
is 1 or 2 depending on the used line ending: ((10),(13),(13,10) or (10,13))
so:
eol_size=1;
if (xs<siz)
if ((dat[xs+1]==10)||(dat[xs+1]==13))
eol_size=2;
As we do not have access to any input file we can only guess... If you need to generate one see:
Here example of binary file access in VCL (bullets #1,#2,#3):