2012-07-04 4 views
1

코드 아래의 생성을 기반으로 파일 이름 : 대상 디렉토리, 라디오 방송국 이름과 현재 시간 :C에 공백없이 파일 이름을 생성

static int start_recording(const gchar *destination, const char* station, const char* time) 
{ 
    Recording* recording; 
    char *filename; 

    filename = g_strdup_printf(_("%s/%s_%s"), 
     destination, 
     station, 
     time); 

    recording = recording_start(filename); 
    g_free(filename); 
    if (!recording) 
     return -1; 

    recording->station = g_strdup(station); 

    record_status_window(recording); 

    run_status_window(recording); 

    return 1; 
} 

출력 예 :

/home/ubuntu/Desktop/Europa FM_07042012-111705.ogg 

문제 :

동일한 방송국 이름에는 제목에 공백이 포함될 수 있습니다.

Europa FM 
Paprika Radio 
Radio France Internationale 
    ........................... 
Rock FM 

나는 그렇게되기 위해 생성 된 파일 이름 출력에서 ​​공백을 제거하는 데 도움이 원하는 :

/home/ubuntu/Desktop/EuropaFM_07042012-111705.ogg 

(더 복잡한 요구 사항은 모든 불법 문자를 제거하는 것. 파일 이름에서)

감사합니다.

UPDATE

이 작성하는 경우 :

static int start_recording(const gchar *destination, const char* station, const char* time) 
{ 
    Recording* recording; 
    char *filename; 

    char* remove_whitespace(station) 
    { 
     char* result = malloc(strlen(station)+1); 
     char* i; 
     int temp = 0; 
     for(i = station; *i; ++i) 
      if(*i != ' ') 
      { 
       result[temp] = (*i); 
       ++temp; 
      } 
     result[temp] = '\0'; 
     return result; 
    } 

    filename = g_strdup_printf(_("%s/%s_%s"), 
     destination, 
     remove_whitespace(station), 
     time); 
    recording = recording_start(filename); 
    g_free(filename); 
    if (!recording) 
     return -1; 

    recording->station = g_strdup(station); 
    tray_icon_items_set_sensible(FALSE); 

    record_status_window(recording); 

    run_status_window(recording); 

    return 1; 
} 

이 경고 가져 오기 :

경고 : '나 strlen'의 인수 하나를 전달하는 [기본적으로 활성화] 캐스트없이 정수의 포인터를 만드는 을 경고 : 캐스팅없이 정수에서 포인터를 할당합니다. [기본적으로 사용 가능]

+1

왜 공백 문자를 파일 이름에 잘못된 문자로 간주합니까? – alk

답변

1
void remove_spaces (uint8_t*  str_trimmed, 
        const uint8_t* str_untrimmed) 
{ 
    size_t length = strlen(str_untrimmed) + 1; 
    size_t i; 

    for(i=0; i<length; i++) 
    { 
    if(!isspace(str_untrimmed[i])) 
    { 
     *str_trimmed = str_untrimmed[i]; 
     str_trimmed++; 
    } 
    } 
} 

공백, 줄 바꿈, 탭 등 하는 것으로이 또한 복사 널 종료 문자 캐리지 리턴을 제거합니다.

1

이 같은 것을 사용할 수 있습니다 : 또 다른 문자열을 같은 크기의 malloc,

char *remove_whitespace(char *str) 
{ 
    char *p; 

    for (p = str; *p; p++) { 
     if (*p == ' ') { 
     *p = '_'; 
     } 
    } 
} 

그렇지 않은 경우, 사용 후 원래의 그것에 문자열과 무료로 복사합니다.

+1

이것은'replace_whitespace'라는 이름이어야하고 다른 isspace() 문자들도 처리해야합니다. – Jens

+2

이것은 아마도 합리적 일지 모르지만 "a b"와 "a_b"가 모두 "a_b"가된다는 것을 언급 할 가치가 있으므로 매핑 후에 충돌하는 입력이있을 가능성이 있습니다. "strdup()는 하나의 호출에서 멋지게 수행합니다. –

관련 문제