terminal에서 < (파일 이름) 입력했을 때 해당 파일을 어떻게 열 수 있나요?

조회수 660회

예를 들어 p1이라는 프로그램을 실행할 때

./p1 < file_name.txt

이런 식으로 입력한 후에 해당 파일 이름을 받아와서 처리하도록 프로그램을 작성하고 싶은데 argv로 넘어가지 않는 것인지 < file_name.txt를 건너뛰고 다음에 입력한 부분부터 argv에 들어갑니다

< file_name.txt 부분은 바꾸지 않고 이 방법으로 파일을 읽어들이고 싶은데 어떻게 처리를 해야 할까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 스택오버플로우 동일 질문


    I'm wondering if there is a way to tell if a redirection is present in the shell command?

    쉘 명령어에 리다이렉션을 알려줄 방법이 있는지 궁금합니다
    

    No. From your program's point of view there is no difference between these two cases:

    ./Myprogram < input.txt

    ./Myprogram

    In both the cases the program is not taking any command line argument and would get it's input from the standard input. In the first case it's the shell that is connecting the contents of the file input.txt to the stdin of your program your program knows nothing about this.

    없습니다. 프로그램의 관점에서 볼 때 
    아래 두 가지 경우에는 차이가 없습니다.
    두 예시:
    
    ./Myprogram < input.txt
    ./Myprogram
    
    두 경우 모두 프로그램은 명령 행 인수를 획득하지 않으며
    리다이렉션은 표준입력에서 입력을받습니다.
    첫 번째 경우는 input.txt 파일의 내용을 
    프로그램의 stdin에 연결하는 쉘입니다. 
    프로그램은 이 사실에 대해 아무 것도 모릅니다.
    
    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)