下面的代码根据目标目录、电台名称和当前时间生成文件名:
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
(更复杂的要求是从文件名中消除所有非法字符)
谢谢你。
更新
如果这样写:
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 使指针从没有强制转换的整数 [默认启用] 警告:赋值使指针从没有强制转换的整数 [默认启用]