我正在开发一个文件模糊器,它可以改变音乐文件并将它们提供给 iTunes。我已经开发了代码来改变音乐文件,但我想使用多个音乐文件来增加代码覆盖率。所以我想遍历我的 iTunes 库并将文件路径加载到缓冲区或文件中;无论哪种方式都足够了。我在这里有这段代码,它在fprintf
函数上出现了段错误。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <fts.h>
int compare (const FTSENT**, const FTSENT**);
int main(void)
{
FILE * musicPaths;
musicPaths = fopen("Users/NahNig/music_paths", "wb");
char *p1 = "/Users/NahNig/Music/iTunes/iTunes Media/Music";
char *p2 = NULL;
char *path[1];
path[0] = p1;
path[1] = p2;
FTS* file_system = NULL;
FTSENT* child = NULL;
FTSENT* parent = NULL;
file_system = fts_open(path, FTS_COMFOLLOW | FTS_NOCHDIR, &compare);
if (NULL != file_system)
{
while( (parent = fts_read(file_system)) != NULL)
{
child = fts_children(file_system,0);
if (errno != 0)
{
perror("fts_children");
}
while ((NULL != child)
&& (NULL != child->fts_link))
{
child = child->fts_link;
fprintf(musicPaths, "%s%s\n", child->fts_path, child->fts_name);
}
}
fts_close(file_system);
}
return 0;
}
int compare(const FTSENT** one, const FTSENT** two)
{
return (strcmp((*one)->fts_name, (*two)->fts_name));
}
我尝试使用 sscanf 和 fprintf 将孩子写入缓冲区并使用 fwrite 尝试写入文件;他们都没有工作。我在谷歌上找到的也没有多大帮助,所以任何帮助都将不胜感激。