C언어 프로그램이 진행이 안돼고 정지되네요

stargt의 이미지

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *in, *out; /* 파일 입출력을 위한 두 개의 파일 포인터 선언 */
    int ch;
    char name[20];  /* 출력 파일 이름 */ 
    
    int count = 0;
    if (argc < 2) /* 입력 파일이 있는지 검사 */
       printf("Sorry, I need the filename for agrument\n");
    else {
       if (( in = fopen(argv[1], "r")) != NULL){
          strcpy(name, argv[1]);
            /* 출력 파일 이름을 생성 */
          strcat(name, ".out");
            /* 출력 파일 이름에 결과물임을 나타내기 위하여 '.out' 추가 */
          out = fopen (name, "w");
            /* 기록하기 위하여 파일을 연다 */
          while ((ch = getch(in)) != EOF)
             if (count ++ %3 == 0)
                  /* 각 3번째 문자마다 출력 - 3으로 나눈 나머지가 0일 때만 출력 */
                putc(ch, out);
             fclose(in);
             fclose(out);
          } else
             printf("I can't find the file %s \n", argv[1]);
    }
}

위의 코드대로 작성했는데요, 도스에서

[프로그램명] [커맨드1]

이렇게 정상적으로 쓰고 엔터치면

저대로 계속 멈춰있습니다.

진행이 안되고요(종료가 안됨)

도데체 왜그런거죠

litdream의 이미지

리눅스에서는 getch 대신에 fgetc 를 해보세요.

삽질의 대마왕...

stargt의 이미지

윈도우예요 윈도우 그런데 안되네요

#include <stdio.h>

int main()
{
while(1)
printf("0");
return 0;
}

익명 사용자의 이미지

:twisted:
while u run, while run

leigh의 이미지

getch는 인자를 안받을텐데.... 저게 컴파일이 되나요?

fgetc를 쓰시는 것이 좋을 듯 싶네요. getch는 이식성이 없습니다.

...

markboy의 이미지

MSDN 에 보면 getch() 함수는 console에서 한글자 읽어오는 함수라고 나옵니다. leigh 님 말씀대로 인자도 없고요.

fgetc를 쓰시는게 맞는 것 같습니다. :D

stargt의 이미지

음 되는 군요.

fgetc로 바꾸니까...

그런데 안바꿨을떄도 컴파일이 되었고 책에는 getch쓰라고 나와있는데 무슨 사태죠?

#include <stdio.h>

int main()
{
while(1)
printf("0");
return 0;
}

lovian의 이미지

stargt wrote:
음 되는 군요.

fgetc로 바꾸니까...

그런데 안바꿨을ㅤㄸㅒㅤ도 컴파일이 되었고 책에는 getch쓰라고 나와있는데 무슨 사태죠?

저자가 실행도 안해보고 올려둔 코드라는 거죠 -_-;;

그런데 VS6으로 돌려봤는데 컴파일 에러 제대로 나오는군요.

어찌된 일일꼬. :)

-----------------
한글을 사랑합니다.

doldori의 이미지

lovian wrote:
stargt wrote:
음 되는 군요.

fgetc로 바꾸니까...

그런데 안바꿨을ㅤㄸㅒㅤ도 컴파일이 되었고 책에는 getch쓰라고 나와있는데 무슨 사태죠?

저자가 실행도 안해보고 올려둔 코드라는 거죠 -_-;;

그런데 VS6으로 돌려봤는데 컴파일 에러 제대로 나오는군요.

어찌된 일일꼬. :)


혹시 C++로 컴파일하지 않으셨나요? ^^;
익명 사용자의 이미지

while ((ch = getch(in)) != EOF)
if (count ++ %3 == 0)
/* 각 3번째 문자마다 출력 - 3으로 나눈 나머지가 0일 때만 출력 */
putc(ch, out);

=======>

while ((ch = getch(in)) != EOF)
{
if (count ++ %3 == 0)
/* 각 3번째 문자마다 출력 - 3으로 나눈 나머지가 0일 때만 출력 */
putc(ch, out);
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.