비주얼 스튜디오에서 Input과 Output을 파일에서 직접 입력, 출력 하는 방법
Visual Studio를 사용해서 알고리즘 연습혹은 구현하다 보면 TC (Test Case)의 데이터를 파일로 부터 읽어올 일이 생긴다.
예를 들면 아래코드에서 주석 처리된 부분, input.txt를 freopen을 이용해서 stdin으로 출력 할 수 있지만,
Visual Studio Project properties에 Command line을 설정해서 간단하게 처리 할 수 있다.
#include <stdio.h>
int main(void)
{
int T;
int N, K;
//freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for (int tcIdx = 1; tcIdx <= T; ++tcIdx)
{
scanf("%d\n", &N);
scanf("%d\n", &K);
printf("%d, %d, %d\n", tcIdx, N, K);
}
return 0;
}
Visual Studio Menu -> Project -> (your project name) Properties... -> Configuration Properties -> Debugging -> Command Arguments 를 보면 텍스트 필드가 있는데 아래처럼 적는다.
< input.txt
만약 Console에 출력되는 값을 파일로 저장하고 싶다면? 이때도 Command Arguments 을 이용하면 된다.
< input.txt > output.txt
이렇게 하면 입력은 input.txt로 출력은 output.txt로 한다.
++ 추가
setbuf를 썼는데 빌드 할 때 아래와 같은 에러가 발생하면.
error C4996: 'setbuf': This function or variable may be unsafe. Consider using setvbuf instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Visual Studio Menu -> Project -> (your project name) Properties... -> Configuration Properties -> C/C++ ->Preprocessor -> Preprocessor Definitions 에 _CRT_SECURE_NO_WARNINGS을 추가 해주자.
Visual Studio Menu -> Project -> (your project name) Properties... -> Configuration Properties -> Linker -> System -> SubSystem 을
Console (/SUBSYSTEM:CONSOLE) 으로 하면 프로그램 실행 이후에도 콘솔창이 사라지지 않는다.