/*square.c */ /*display a hollow square on the screen */ /*author: Weiwei Chen */ /*date: Nov.27.2010 */ #include #define SLEN 80 FILE *fout; void square(int s) { int r, i; for(r = 0; r < s; r ++ ) { if(r == 0 || r == s - 1) { for(i = 0; i < s; i ++) { printf("*"); fprintf(fout, "*"); } printf("\n"); fprintf(fout, "\n"); } else { printf("*"); fprintf(fout, "*"); for(i = 0; i < s - 2; i ++) { printf(" "); fprintf(fout, " "); } printf("*\n"); fprintf(fout, "*\n"); } } } int main() { char str[SLEN]; printf("Please input the file name: "); scanf("%s", str); fout = fopen(str, "w"); if(fout) { square(10); } else{ printf("Failed to open file \"%s\"! \n", str); } fclose(fout); return 0; }