본문 바로가기
  • 소소한 개발자 이야기
Software/알고보면 쓸모있는 코딩스킬

C Language 파일 입출력

by Siwan_Min 2020. 6. 26.
728x90

알고리즘같은 문제를 풀 때 유용한 기능 스킬입니다! 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include
 <stdio.h>
 
int arr[100][100];
int n, m;
 
void Input() {
    
    scanf("%d %d"&n, &m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf("%d"&arr[i][j]);
        }
    }
}
 
void Output() {
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", arr[i][j]);
        }printf("\n");
    }
    
}
 
int main() {
    
    
    freopen("Input.txt""r", stdin);
    freopen("output.txt""w", stdout);
    Input();
    Output();
    
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    }
    
    fclose(stdin);
    fclose(stdout);
    
    return 0;
 
 
}

위와 같이 읽어들을 텍스트 파일을 freopen("Input.txt", "r", stdin) 으로 설정하고 scanf()를 하면 메모장에 적혀있는 데이터들을 읽어들어 옵니다. 출력은 freopen("output.txt", "w", stdout)을 하고 printf()를 하면 output.txt 파일에 데이터 값이 출력이 됩니다. 

삼성 EXPERT 아카데미 문제 보면 input.txt 파일있죠?

비주얼 스튜디오로 문제 풀때 테스트케이스 input.txt 파일을 이런식으로 불러들어오면 편할 것 같네요 !!

 

 

728x90

댓글