我對C編程語言相當陌生。我正在編寫一個程序,最終將通過用戶輸入目錄中的mp3文件讀取,並通過利用id3中的元數據將mp3分類到藝術家/專輯文件夾中。我使用system()函數調用訪問用戶的目錄,並生成一個包含該目錄中所有mp3的.txt文件。但是,當我試圖訪問第一個mp3文件時遇到了問題。我正在構建mp3文件的路徑,但該文件無法打開。當我硬編碼路徑時,該文件會打開。C編程 - 從特定的Windows目錄打開
#include <stdio.h>
#include <string.h>
struct FILE_head
{
char file_id[3];
char version[2];
char flags;
char size[4];
};
int main()
{
//declare vars
char cd[1200];
char mp3[200];
char dir[1000];
char mp3_path[1200];
FILE *list_file;
FILE *mp3_file;
struct FILE_head id3;
char dir_cmd[1300] = "dir ";
char find_cmd[100] = "/b | find \".mp3\" > \"mp3List.txt\"";
int dir_len;
int amt_read;
//main code
while(1)
{
cd[0]='c';
cd[1]='d';
cd[2]=' ';
cd[3]='\0';
printf("Enter the directory where mp3's are located:");
scanf("%s", dir);
strcat(cd, dir);
if(system(cd) == 0) //if directory is valid, break. otherwise stay in loop/reprompt
break;
printf("Valid directory Ex--> c:\\users\\username\\music\n");
}
//build cmd statement
strcat(dir_cmd, dir);
strcat(dir_cmd, find_cmd);
system(dir_cmd);
dir_len = strlen(dir);
strcpy(mp3_path, dir);
printf("%s\n", mp3_path);
list_file = fopen("mp3List.txt", "rb");
if(list_file != NULL)
{
while(fgets(mp3, sizeof(mp3), list_file))
{
printf("%s", mp3);
strcat(mp3_path, mp3);
printf("%s\n", mp3_path);
mp3_path[strlen(mp3_path)-1] = '\0';
mp3_file = fopen(mp3_path, "rb");
if(mp3_file != NULL)
{
printf("in this loop");
fread(&id3, sizeof(id3), 1, mp3_file);
printf("%s\n", id3.file_id);
}
}
}
return 0;
}
這是我第一次發佈,所以如果有更多的信息會有幫助,請讓我知道。我意識到可能有更好的方式來訪問目錄,但我不想使用任何不在std lib中的函數。
這是我的輸出:
C:\用戶\米切爾\項目> mp3sort.exe輸入在MP3的 所在的目錄:C:\用戶\米切爾\項目\ music_test \ C: \用戶\米切爾\項目\ music_test \ 01 - 時間Pretend.mp3 C:\用戶\米切爾\項目\ music_test \ 01 - 時間Pretend.mp3
01-all_that_remains-this_calling.mp3 01-all_that_remains -this_calling.mp3t \ 01 - 預約時間.mp3
07比利·喬爾 - 人人都愛你Now.mp3 07比利·喬爾 - 每個人都 愛你Now.mp3Time到Pretend.mp3
淚的Sheep.mp3淚的Sheep.mp3dy愛你 Now.mp3Time到Pretend.mp3
C:\用戶\米切爾\項目>
我知道,我現在的建立後的第一次的全部文件是不正確的,但我只是集中於得到第一mp3_path工作。我刪除了換行符:mp3_path[strlen(mp3_path)-1] = '\0';
很可能在創建路徑時存在一些錯誤。你有沒有嘗試過使用調試器? – Codor 2014-10-06 06:21:58
我一直在使用printf來檢查mp3_path。儘管我現在會嘗試調試器。 – mcleric76 2014-10-06 06:27:50
你的程序的輸出是什麼? – 2014-10-06 07:04:39