2013-10-29 120 views
0

我正在开发一个文件模糊器,用于改变音乐文件并将它们提供给iTunes。我已经开发了用于改变音乐文件的代码,但我想使用多个音乐文件来增加代码覆盖率。所以我想遍历我的iTunes库并将文件路径加载到缓冲区或文件中;无论哪种方式就足够了。我在这里有代码函数的段错误。将fts(3)的输出写入文件

#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尝试写入文件试图;他们都没有工作。我在谷歌上找到的东西也没有太多帮助,所以任何帮助将不胜感激。

回答

0

你没有测试从fopen()的返回值,因为你从/Users/NahNig/music_paths错过了第/是NULL。当您使用空指针时,程序崩溃。