C언어에서 파일 쓰기가 안됩니다. ㅜ

갈길이멀다...의 이미지

#include
#include

int main(void){
FILE *fp_t, *fp_b;
char c;
int i=0;
if((fp_t=fopen("test.txt", "rt")) == NULL){
printf("file open error\n");
}
if(fp_b=fopen("test.bin","wb")==NULL){
printf("file open error\n");
}
while(1){
c=fgetc(fp_t);
if(c==EOF) break;
printf("check %c\n",c);
fwrite(&c,sizeof(char),1,fp_b);
}
fcloseall();
return 0;
}

텍스트를 바이너리로 변화해야되서 코드를 짰는데
딱히 에러날 구문이 없는데
fwrite에서 세그멘테이션 오류가 발생합니다.
바이너리 모드라서 그런가보다 생각해서
텍스트 모드의 fputc, fputs 역시 오류가 발생하는데
이 간단한 코드 에러를 도저히 모르겠습니다. ㅜ

shint의 이미지


    FILE *fp_t;
    FILE *fp_b;
    char c;
    int i=0;
    if((fp_t=fopen("test.txt", "rt")) == NULL)
    {
        printf("file open error\n");
        return 0;
    }
    if((fp_b=fopen("test.bin","wb"))==NULL)
    {
        printf("file open error\n");
        return 0;
    }
 
    while(1)
    {
        c=fgetc(fp_t);
        if(c==EOF)
        {
            break;
        }
        printf("check %c\n",c);
        fwrite(&c,sizeof(char),1,fp_b);
    }
    fclose(fp_t);
    fclose(fp_b);

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

갈길이멀다...의 이미지

이런 말도 안되는 실수에
엄청나게 시간 낭비했네요 ㅜㅜ

정말 감사합니다.

그런데 바이너리로 변환했는데도
그냥 에디터로 읽히네요 ;;

변환이 되면 원래 깨져서 나오지 않나요?

익명 사용자의 이미지

괄호를 빼먹은 것이 문제였으면 컴파일에러가 나야지 세그멘테이션 폴트가 나는 것이 이상하군요.
여하튼, 이 코드는 바이너리로 변화하는 코드가 아닙니다.
문자 하나씩 읽어서 그대로 저장할 뿐인데 달라질 것이 하나도 없지요.
출력된 파일 내용을 입력 파일과 비교해보세요. 똑같을 겁니다.
보통 "바이너리로 변환한다"고 하면, 예를 들어
텍스트로 12345\t1110 라고 되어 있는 것을 (\t 는 구분자 용도로 썼습니다.)
12345 를 타나내는 int 하나(4byte)를 쓰고,
1110을 나타내는 int 하나(4byte)를 쓰는 식으로 변환하는 것이지요.
그러면 원래 10byte (문자 10개)로 표현되었던 내용이 8byte 로 바뀌겠지요.
물론 텍스트 에디터로 읽으면 이상한 문자들이 나올테구요.

익명 사용자의 이미지

연산자 우선순위를 다시 공부하셔야 할 듯.
문법상으로 전혀 하자 없으니까 컴파일 에러가 안나왔겠죠.

익명 사용자의 이미지

괄호를 한쪽만 빼먹은게 아니라 양쪽 다 빼먹은거라고 쓰면 간단한걸
꼭 이렇게 다른사람 바보만들어야 속이 시원하냐?

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.