ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C 프로그래밍: 새내기를 위한 첫 C 언어 책] 실습 15주차
    학교 수업/1-1, 1-2 코딩 기초 (파이썬, C) 2020. 12. 7. 17:23
    반응형

     

     

     

    문제1

     

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main()
    {
    	FILE *fp1, *fp2;
    	int ch;
    
    	fp1 = fopen("from.txt", "rt");
    	fp2 = fopen("to.txt", "wt");
        
    	while ((ch = fgetc(fp1)) != EOF)
    		fputc(ch, fp2);
    
    	if (feof(fp1))
    		printf("복사 완료\n");
    	else
    		printf("복사 에러\n");
    
    	fclose(fp1);
    	fclose(fp2);
    
    	return 0;
    }

     

     

     

    문제 2

     

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int main()
    {
    	FILE* fp1, * fp2;
    	char ch[100];//버퍼배열
    
    	fp1 = fopen("from.txt", "rt");
    	fp2 = fopen("to.txt", "wt");
    
    	while ((fgets(ch,100,fp1)) != NULL)
    		fputs(ch, fp2);
    
    	if (feof(fp1))
    		printf("복사 완료\n");
    	else
    		printf("복사 에러\n");
    
    	fclose(fp1);
    	fclose(fp2);
    
    	return 0;
    }

     

    문제3

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 3
    
    struct person
    {
    	int id;
    	char name[20];
    };
    typedef struct person PERSON;
    
    int main()
    {
    	int i, count, x;
    	PERSON arr[MAX];
    
    	
    	FILE* out = fopen("student.bin", "wb");
    
    	//file error
    	if (out == NULL)
    	{
    		printf("File open error\n");
    		return 0;
    	}
    
    	
    	for (i = 0; i < MAX; i++)
    	{
    		printf("\n%d, 이름: ", i + 1); scanf("%s", arr[i].name);
    		printf("   번호: "); scanf("%d", &arr[i].id);
    	}
    	for (i = 0; i < MAX; i++)
    	{
    		count = fwrite(&arr[i], sizeof(arr[i]), 1, out);
    		
    		if (count < 1)
    		{
    			printf(stderr, "Error opening while a structure. \n");
    			return 0;
    		}
    	}
    	fclose(out);
    	printf("\n== 파일 출력 완료 ==\n");
    
    	return 0;
    }

     

     

    문제4

     

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 3
    
    struct person
    {
    	int id;
    	char name[20];
    };
    typedef struct person PERSON;
    
    int main()
    {
    	int count=0;
    	PERSON arr[MAX];
    
    	FILE* infile = fopen("student.bin", "rb");
    	
    	if (infile == NULL)
    	{
    		printf("FILE OPEN ERROR\n");
    		return 0;
    	}
    	
    	
    	while (fread(&arr[count], sizeof(arr[count]), 1, infile) == 1)
    		count++;
    
    	
    	if (feof(infile)) printf("Reading complete\n");
    	else printf("Reading error\n");
    
    	
    	for (int i = 0; i < MAX; i++)
    		printf("id: %d, name: %s\n", arr[i].id, arr[i].name);
    
    	fclose(infile);
    	return 0;
    }

     

     

    문제 5

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #define WIDTH  256
    #define HEIGHT 256
    
    int main()
    {
    	int i, j;
    	unsigned char Org[WIDTH][HEIGHT];
    	unsigned char Out[WIDTH][HEIGHT];
    
    	FILE* infile = fopen("binary image/lena.raw", "rb");
    	if (infile == NULL)
    	{
    		printf("파일 열기 실패\n");
    		return 0;
    	}
    	fread(Org, sizeof(char), WIDTH * HEIGHT, infile);
    	fclose(infile);
    
    	for(i=0; i<HEIGHT; i++)
    		for (j = 0; j < WIDTH; j++)
    		{
    			Out[i][j] = 255 - Org[i][j];
    		}
    	FILE* outfile1 = fopen("lena_inv.raw", "wb");
    	fwrite(Out, sizeof(char), WIDTH * HEIGHT, outfile1);
    	fclose(outfile1);
    
    	for (i = 0; i < HEIGHT; i++)
    		for (j = 0; j < WIDTH; j++)
    		{
    			Out[i][j] = Org[i][WIDTH-j];
    		}
    	FILE* outfile2 = fopen("lena_1.raw", "wb");
    	fwrite(Out, sizeof(char), WIDTH * HEIGHT, outfile2);
    	fclose(outfile2);
    
    	for (i = 0; i < HEIGHT; i++)
    		for (j = 0; j < WIDTH; j++)
    		{
    			Out[i][j] = Org[HEIGHT-i][j];
    		}
    	FILE* outfile3 = fopen("lena_2.raw", "wb");
    	fwrite(Out, sizeof(char), WIDTH * HEIGHT, outfile3);
    	fclose(outfile3);
    
    	return 0;
    }
    반응형

    댓글

Designed by Tistory.