2012-12-07 2 views
3

나는 node.js의 C++ addon (.cc) 파일에서 어떻게 파일을 만들고 데이터를 추가 할 수 있는지 알고 싶습니다. ??node.js의 C++ addon에 텍스트 파일 만들기

아래 코드를 사용하여 동일한 작업을 수행했지만 내 우분투 컴퓨터에서 "data.txt"파일을 찾을 수 없습니다. (코드 뒤에있는 이유는 파일을 만드는 올바른 방법이 아니라 이상한 것입니다. 컴파일시에 오류/경고를 받았습니다).

FILE * pFileTXT; 

pFileTXT = fopen ("data.txt","a+"); 

const char * c = localReq->strResponse.c_str(); 

fprintf(pFileTXT,c); 

fclose (pFileTXT); 
+1

std :: ofstream을 사용하시기 바랍니다. – Aleph

답변

7

Node.js를가 libuv에 I/O (비동기 여부를) 처리하는 C 라이브러리를 사용합니다. 이렇게하면 이벤트 루프를 사용할 수 있습니다.

당신은 libuv이 무료 온라인 책/도입에 관심이있을 것 : http://nikhilm.github.com/uvbook/index.html

는 특히, reading/writing files에 전념 한 장있다.

int main(int argc, char **argv) { 
    // Open the file in write-only and execute the "on_open" callback when it's ready 
    uv_fs_open(uv_default_loop(), &open_req, argv[1], O_WRONLY, 0, on_open); 

    // Run the event loop. 
    uv_run(uv_default_loop()); 
    return 0; 
} 

// on_open callback called when the file is opened 
void on_open(uv_fs_t *req) { 
    if (req->result != -1) { 
     // Specify the on_write callback "on_write" as last argument 
     uv_fs_write(uv_default_loop(), &write_req, 1, buffer, req->result, -1, on_write); 
    } 
    else { 
     fprintf(stderr, "error opening file: %d\n", req->errorno); 
    } 
    // Don't forget to cleanup 
    uv_fs_req_cleanup(req); 
} 

void on_write(uv_fs_t *req) { 
    uv_fs_req_cleanup(req); 
    if (req->result < 0) { 
     fprintf(stderr, "Write error: %s\n", uv_strerror(uv_last_error(uv_default_loop()))); 
    } 
    else { 
     // Close the handle once you're done with it 
     uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL); 
    } 
} 

node.js에 C++을 쓰려면이 책을 읽는 데 시간을 할애해야합니다. 그것은 가치.

+0

고마워요 .. !! 언제든지 책을 살펴보고 알려 드리겠습니다 .. !! – Mayur