作者在 2013-05-13 15:49:27 发布以下内容
/*
创建一个包含20行,每行30个整数的文本文件.
整数在0到9之间,用空格分开.该文件是一个图片的数字表示,
从0到9的值代表逐渐增加的灰度.
编写一个程序,将文件的内容读入到一个20 * 30 的int数组中.
一种将这种数字表示转化成图片的粗略方法就是让程序使用数组中的数值来初始化一个
20 * 31 的字符阵列.
0对应空格字符,1对应句号字符,依此类推,较大的值对应占用空间较多的字符.
比如,可以使用#代表9.
每行的最后一个字符(第31个)为空字符,这样数组将包含20个字符串.
然后程序显示结果图片(即打印这些字符串),
并将结果存入一个文本文件中
例如:如果开始的数据为:
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 0 4 5 2 0 0 0 0 0 0 0 0
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 4 5 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 1 8 5 0 0 0 4 5 2 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 4 5 2 0 0 0 0 0
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 3 9 9 9 9 9 9 9
8 8 8 8 8 8 8 8 8 8 8 8 5 8 9 9 8 5 8 8 8 8 8 8 8 8 8 8 8 8
5 5 5 5 5 5 5 5 5 5 5 5 5 8 9 9 8 5 5 5 5 5 5 5 5 5 5 5 5 5
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 2 2 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 3 3 0 0 0 0 0 0 5 8 9 9 8 5 0 5 6 1 1 1 1 6 5 0 0 0
0 0 0 0 4 4 0 0 0 0 0 0 5 8 9 9 8 5 0 0 5 6 0 0 6 5 0 0 0 0
0 0 0 0 5 5 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 6 6 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ROWS 20
#define INT_ROLS 30
#define CHAR_ROLS 31
#define BUFSIZE 60
#define CHAR_TO_INT 48
void saveToArray(FILE *, int [][INT_ROLS]);
void change(int [][INT_ROLS], char [][CHAR_ROLS]);
int main(int argc, char *argv[])
{
int sources[ROWS][INT_ROLS] = {0};
char target[ROWS][CHAR_ROLS];
FILE *fp;
int i, j;
if((fp = fopen("cpp_13.11_12.txt", "r")) == NULL)
{
fprintf(stderr, "Can't open file :\"cpp_13.11_12.txt\".\n");
exit(1);
}
saveToArray(fp, sources);
fclose(fp);
change(sources, target);
/* printf picture */
for(i = 0; i < 20; i++)
puts(target[i]);
/*write file*/
if((fp = fopen("char.txt", "w")) == NULL)
{
fprintf(stderr, "Can't create file :\"char.txt\".\n");
exit(2);
}
for(i = 0; i < 20; i++)
{
fputs(target[i], fp);
putc('\n', fp);
}
fclose(fp);
printf("\n\nDone.\n");
return 0;
}
void saveToArray(FILE * fp, int sources[][INT_ROLS])
{
int *psl;
char tmp[BUFSIZE];
char *p;
psl = *sources;
while(fgets(tmp, BUFSIZE, fp) != NULL) /*从文件读入数字,存入INT数组*/
{
p = tmp;
while(*p)
{
if(*p != ' ' && *p != '\n')
*psl++ = (int)*p - CHAR_TO_INT;
p++;
}
}
}
void change(int sources[][INT_ROLS], char target[][CHAR_ROLS])
{
int (*psw)[INT_ROLS];
int *psl;
char (*ptw)[CHAR_ROLS];
char *ptl;
for(psw = sources, ptw = target; psw < sources + ROWS; psw++, ptw++)
{
for(psl = *psw, ptl = *ptw; psl < *psw + INT_ROLS; psl++, ptl++)
{
switch(*psl){
case 0:*ptl = ' ';break;
case 1:*ptl = '.';break;
case 2:*ptl = '\'';break;
case 3:*ptl = ':';break;
case 4:*ptl = '~';break;
case 5:*ptl = '*';break;
case 6:*ptl = '=';break;
case 7:*ptl = '!';break;
case 8:*ptl = '%';break;
case 9:*ptl = '#';break;
}
}
*ptl = '\0';
}
}