초보적인 질문.. ^^a

leilei의 이미지

아래와 같이 하면 main() 에서 LogOpen()을 호출한 다음
logfp를 사용할려고 하면 그냥 NULL 입니다..
사용할려면 어떻게 해야 하죠??
전역변수로 하지 않을려고 하는데...

int LogOpen(FILE *fp)
{
        char filename[32];

        ............

        if((fp=fopen(filename, "a")) == NULL)
        {
                perror("LogOpen ::  fopen error : ");
                return -1;
        }


        return 0;
}

main()
{
        FILE *logfp = NULL;
        int len, i;
        char buf[256];

        if(LogOpen(logfp) < 0) exit(1);
 
         .....................
}


cdpark의 이미지

질문 올리기 전에 C 언어 책을 읽어보세요. :(

저라면 FILE *LogOpen() 식으로 짜겠습니다.

초코리의 이미지


#include <stdio.h>
                                                                                
int LogOpen(FILE **fp){
        if((*fp = fopen("test_file","aw"))==NULL){
                perror("error");
                return -1;
        }
        return 0;
}
int main(void){
        FILE *logfp = NULL;
                                                                                
        if((LogOpen(&logfp))==-1) printf("error1");
        fprintf(logfp,"초코리");
        close(logfp);
        return 0;
}
                                                                                


인자로 넘겨서 받고 싶다면 이러면 되지않을까요?

datamind의 이미지

코드는 초코리님이 올리신것처럼 수정하면 되고요,,
이유는,
함수호출시 포인터를 인자로 넘겨주시게 되면,
인자값은 새로운 변수로 복사되어 전달되게 됩니다.
그러므로, 호출된 함수내에서 인자값을 수정하면,
새로이 복사된 값을 수정하는 것이 되어,
호출한 함수에서는 반영이 안됩니다.

그러므로,
이럴때에는 포인터의 포인터를 사용하셔야 합니다.
포인터를 인자값으로 넘겨주실 때,
포인터의 포인터를 넘겨줌으로서 해결을 할수 있습니다.
자세한 사용법은 포인터 사용법을 찾아보세요.. -_-;;


#include <stdio.h> 
                                                                                
int LogOpen(FILE **fp)    // <- 포인터의 포인터를 받습니다. 
{ 
        // *fp 로 사용하여야지 호출한 함수에서 반영이 됩니다. 
        if((*fp = fopen("test_file","aw"))==NULL){ 
                perror("error"); 
                return -1; 
        } 
        return 0; 
} 

int main(void)
{ 
        FILE *logfp = NULL; 
                                             
        // &logfp 로서 포인터의 포인터를 넘겨줍니다.                                    
        if((LogOpen(&logfp))==-1) printf("error1"); 
        fprintf(logfp,"초코리"); 
        close(logfp); 
        return 0; 
}                                                                               

댓글 달기

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