2010-04-28 7 views
2

gcc 4.4.1읽기 기능을 사용하여 파일을 읽는 중

나는 읽기 기능을 사용하여 웨이브 파일을 읽습니다. 그러나, 그것은 읽기 기능을 얻을 때. 실행이 중단 된 것처럼 보입니다. 내가이 일을 잘못하고 있는지 궁금해합니다.

파일 크기 test-short.wave는 514K입니다.

내가 목표로 한 것은 한 번에 파일을 메모리 버퍼로 읽어들입니다. 현재 나는 이것을 테스트하고 있습니다. 어떤 제안에 대한

많은 감사,

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <string.h> 
#include <unistd.h> 

int main(void) 
{ 
    char buff = malloc(10240); 
    int32_t fd = 0; 
    int32_t bytes_read = 0; 

    char *filename = "test-short.wav"; 

    /* open wave file */ 
    if((fd = (open(filename, O_RDWR)) == -1)) 
    { 
     fprintf(stderr, "open [ %s ]\n", strerror(errno)); 
     return 1; 
    } 
    printf("Opened file [ %s ]\n", filename); 
    printf("sizeof(buff) [ %d ]\n", sizeof(buff)); 

    bytes_read = read(fd, buff, sizeof(buff)); 

    printf("Bytes read [ %d ]\n", bytes_read); 

    return 0; 
} 

=== 편집 수정의 ===

#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <fcntl.h> 
#include <string.h> 
#include <unistd.h> 

int main(void) 
{ 
    char buff[10240] = {0}; 
    int32_t fd = 0; 
    int32_t bytes_read = 0; 
    const char *filename = "test-short.wav"; 

    fd = open(filename, O_RDWR); 
    if(fd == -1) 
    { 
    fprintf(stderr, "open [ %s ]\n", strerror(errno)); 
    return 1; 
    } 

    printf("sizeof(buff) [ %d ]\n", sizeof(buff)); 
    printf("strlen(buff) [ %d ]\n", strlen(buff)); 

    bytes_read = read(fd, buff, sizeof(buff)); 
    printf("Bytes read [ %d ]\n", bytes_read); 

    return 0; 
} 
+0

기침 libsndfile 기침 –

답변

5
  1. 0123에 대한 포인터이 아니라 char*입니다.
  2. 당신은 sizeof(char) (가능성이 1 바이트), 10240
  3. 당신은 포인터로 변환 어떤 buff로 데이터를 읽을 수 없습니다를 읽을하지 버프로, 가리 킵니다.
  4. Ignacio Vazquez-Abrams가 언급 한 우선 순위 문제는 여전히 관련이 있습니다.
  5. strlen()을 char로 호출하면 별 의미가 없습니다. 완충제로 채워지는 것을 채우기 전에 훨씬 적습니다.
  6. const char * (문자열 리터럴)을 char*에 할당합니다.

이 코드 주위에 컴파일러 경고가 표시되지 않습니까?

+0

나는 고침을하였습니다. 사실 다음과 같이 컴파일합니다 .Wall -Wextra -ggdb – ant2009

+0

편집 된 코드가 작동 할 것으로 기대합니다. 즉, 출력에서 ​​10240, 0 및 10240 (파일이 더 크고 제공된대로 제공)을 제공하십시오. –

4

===보다 높은 우선 순위를 가지고 : 당신은 지정

if((fd = open(filename, O_RDWR)) == -1) 
관련 문제