【发布时间】:2012-07-04 11:30:13
【问题描述】:
下面的代码根据目标目录、电台名称和当前时间生成文件名:
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 使指针从整数而不进行强制转换 [默认启用] 警告:赋值使指针从整数不进行强制转换[默认启用]
【问题讨论】:
-
为什么您认为空格字符是文件名的非法字符?
标签: c