我很难在大学任务中找到问题。该程序是一个音乐播放器,它有5个功能,一切正常,但一个,添加歌曲到播放列表。问题在于函数的第二次调用,当它必须将第二首歌曲添加到字符串数组playList时。我注意到调试代码,在我按下fgets()函数后输入的那一刻,它将playList [0]更改为我刚写的内容,甚至没有输入修改数组的函数。所以我认为问题来自除功能之外的其他地方。我使用动态数组,这会好很多但我不允许使用动态内存D :(对不起,如果我拼错了,任何英语都不是我的主要语言)。我相信问题出在函数fill_playlist中,也许当我为playList [i]指定一个字符串但我无法找到位置时。当我在另一个菜单选项Search Search中调用fgets时也会发生这种情况。
我认为这是一个缺少指针算术的问题。
基本上每当我使用fgets()时,playList [0]会立即填充最后写的字符串,并且我无法填充playList [1]或更多。
由于
http://pastebin.com/FX2bHUSk
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#define LIBRARY_DIRECTORY "/home/user/Desktop/library/"
#define MSG_RANDOM_LIST "How many songs would you like to randomly listen?"
#define MSG_RANDOM_ERR "There are not that many songs in the list!"
#define MSG_INPUT_ERROR "There has been an input error!"
#define MSG_SEARCH "These are the songs in the playlist that contain the keyword: "
#define MSG_EMPTY_LIST "The list is empty! You can add records using the 'Add Record' function."
#define MSG_FULL_LIST "The list is full, you have added all the songs in the library!"
#define MSG_ALREADY_IN "The song you intend to add is already on the playlist. Please try with another one."
#define MSG_ADD_RECORD "Write the name of the song you would like to add."
#define MSG_WRONG_NAME "The song you entered is not in the list, please add another one."
#define MSG_SUCCESFUL_ADD "The song you wrote was succesfully added to the playlist!"
#define MSG_SEARCH_RECORD "Enter a keyword to search for a record on the playlist."
#define MSG_EMPTY_SEARCH "There are no songs matching the input keyword."
#define OPT_PLAY_IN_ORDER "0 - Play in order."
#define OPT_PLAY_RANDOM "1 - Play randomly."
#define OPT_PLAY_RECORD "2 - Play record."
#define OPT_ADD_RECORD "3 - Add record."
#define OPT_SEARCH_RECORD "4 - Search record."
#define OPT_LIST_RECORD "5 - List records."
#define OPT_EXIT "6 - Exit."
#define MAX_PLAYLIST 25
#define MAX_RECORD 20
#define MAX_MENU 6
typedef enum {PLAY_IN_ORDER=0,PLAY_RANDOM=1,PLAY_RECORD=2, ADD_RECORD=3, SEARCH_RECORD=4,LIST_RECORD=5,EXIT=6} first_choice_t;
typedef enum {ST_INPUT_ERROR=0,ST_EMPTY_LIST=1,ST_FULL_PLAYLIST=2,ST_ALREADY_IN=3,ST_WRONG_NAME=4,ST_RANDOM_ERR=5,ST_EMPTY_SEARCH=6,ST_EXIT=7,OK=8} status_t;
void initialize_array(char ** array);
char * select_record (char ** playList, int i);
status_t menu_limit(int choice);
void printMenu();
status_t player(char ** playList);
status_t playOrder(char** playList);
status_t validateChoice(int choice, char ** playList);
status_t playRandom(int max_rand,char ** playList);
status_t printPlaylist(char** playList);
void playRecord (char * record);
int limit_random(int max_random);
void cleanstdin();
status_t add_record (char * record,char ** playList);
status_t fill_playlist (char * record,char ** playList);
status_t check_repeat (char * record,char ** playList,int length);
status_t search_record (char * keyword, char ** playList);
int main()
{
/*string array which i'll initialize with 0s to fill with songs later.*/
char * playList [MAX_PLAYLIST];
status_t st= OK;
initialize_array(playList);
srand(time(NULL));
if((st=player(playList))!=OK)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/*prints the main menu of the program*/
void printMenu(){
puts(OPT_PLAY_IN_ORDER);
puts(OPT_PLAY_RANDOM);
puts(OPT_PLAY_RECORD);
puts(OPT_ADD_RECORD);
puts(OPT_SEARCH_RECORD);
puts(OPT_LIST_RECORD);
puts(OPT_EXIT);
}
/*the program itself, depending on the input number continues with the selected function */
status_t player (char ** playList){
int record_choice,max_random,choice;
char aux[MAX_RECORD];
char * record;
char * keyword;
char * err_library[] = {MSG_INPUT_ERROR, MSG_EMPTY_LIST, MSG_FULL_LIST,MSG_ALREADY_IN,MSG_WRONG_NAME,MSG_RANDOM_ERR,MSG_EMPTY_SEARCH};
status_t st=OK;
do{
printMenu();
scanf("%d",&choice);
cleanstdin();
if((st =menu_limit(choice))!=OK){
fprintf(stderr, "%s\n",err_library[st]);
return st;
}
switch(choice)
{
case(PLAY_IN_ORDER):
{
if((st=playOrder(playList))!=OK){
fprintf(stderr,"%s \n", err_library[st]);
}
else
st = ST_EXIT;
break;
}
case(PLAY_RANDOM):
{
puts(MSG_RANDOM_LIST);
scanf("%d",&max_random);
cleanstdin();
if((st=playRandom(max_random,playList))!=OK){
fprintf(stderr,"%s\n",err_library[st]);
break;
}
st=ST_EXIT;
break;
}
case(PLAY_RECORD):
{
if((st=printPlaylist(playList))!=OK){
fprintf(stderr, "%s\n",err_library[st]);
break;
}
scanf("%d",&record_choice);
cleanstdin();
if((st=validateChoice(record_choice,playList))!=OK){
fprintf(stderr, "%s\n", err_library[st]);
break;
}
record = select_record(playList,record_choice);
playRecord(record);
st=ST_EXIT;
break;
}
case(ADD_RECORD):{
printf("%s \n",MSG_ADD_RECORD);
fgets(aux,MAX_RECORD,stdin);
aux[(strlen(aux)-1)]='\0';
record= aux;
if ((st=add_record(record,playList))!=OK)
fprintf(stderr,"%s \n",err_library[st]);
break;
}
case (SEARCH_RECORD):
puts(MSG_SEARCH_RECORD);
fgets(aux,MAX_RECORD,stdin);
keyword= aux;
if((st =search_record(keyword,playList))!=OK)
fprintf(stderr,"%s\n",err_library[st]);
break;
case (LIST_RECORD):
if((st=printPlaylist(playList))!=OK){
fprintf(stderr, "%s\n",err_library[st]);
return st;
}
break;
case(EXIT):
{
return ST_EXIT;
}
default:
{
fprintf(stderr,"%s\n",MSG_INPUT_ERROR);
return ST_EXIT;
}
}
}while (st != ST_EXIT);
return OK;
}
/*Checks that, having six functions, you don't put a 7*/
status_t menu_limit (int choice){
if (choice > MAX_MENU)
return ST_INPUT_ERROR;
return OK;
}
/*Plays the songs in the playlist in order*/
status_t playOrder(char ** playList){
int i;
char * record;
for(i=0;playList[i] != 0 && i <MAX_PLAYLIST; i++){
record = select_record(playList,i);
playRecord(record);
}
if(i == 0)
return ST_EMPTY_LIST;
return OK;
}
/*Checks that, having for example, 6 songs in the playlist, you don't put song n°7*/
status_t validateChoice(int choice,char ** playList){
int i;
for(i=0; playList[i] != 0 && i < MAX_PLAYLIST; i++);
if(choice > MAX_PLAYLIST || choice > i)
return ST_INPUT_ERROR;
return OK;
}
/*Plays the playlist randomly */
status_t playRandom(int max_random,char ** playList){
int i;
status_t st;
char * record;
if ((st = validateChoice(max_random,playList)) != OK){
return ST_RANDOM_ERR;
}
for(i=0;i<max_random;i++){
record = select_record(playList,limit_random(MAX_PLAYLIST));
playRecord(record);
}
return OK;
}
/*print current playlist*/
status_t printPlaylist(char ** playList){
int i;
for(i=0;playList[i] != 0 && i< MAX_PLAYLIST ;i++){
printf("%d -%s \n",i,playList[i]);
}
if (i == 0)
return ST_EMPTY_LIST;
return OK;
}
/*recieves an int and returns the corresponding song (this could be all inside of playrecord)*/
char * select_record (char ** playList, int i){
return playList[i];
}
/*given a string plays the record using mpg123 this function was provided by the one who thought the assignment*/
void playRecord (char * record){
char * cmd[3];
int pid = fork();
cmd[0]="mpg123";
cmd[1]=record;
cmd[2]=NULL;
if (!pid)
execvp(cmd[0],cmd);
waitpid(pid,NULL,0);
}
/*Initializes the array with 0s*/
void initialize_array(char ** playList){
int i;
for(i=0;i<MAX_PLAYLIST;i++){
playList[i]=0;
}
}
int limit_random(int max_random){
int number;
number = (rand()%(max_random+1));
return number;
}
void cleanstdin (void){
int c;
do
{
c=getchar();
}
while(c != '\n' && c != EOF);
}
/*procedures and validations to add a song into the playlsit.*/
status_t add_record (char * record,char ** playList){
status_t st;
char aux[90];
strcpy(aux,LIBRARY_DIRECTORY);
strcat(aux,record);
if (!access(aux, F_OK)){
if((st=fill_playlist(record,playList))!=OK)
return st;
}
else{
return ST_WRONG_NAME;
}
return OK;
}
/* once the user inputs a name, and after making some validations adds the song to the playlist(first position that doesn't have a "0" on it)*/
status_t fill_playlist (char * record,char ** playList){
int i;
status_t st;
char * aux;
aux = record;
for(i=0;playList[i] != 0 && i < MAX_PLAYLIST; i++){}
if (i == MAX_PLAYLIST)
return ST_FULL_PLAYLIST;
if((st=check_repeat(aux,playList,i))!=OK)
return st;
playList[i]=aux;
printf("%s\n",MSG_SUCCESFUL_ADD);
return OK;
}
/* Checks the songs which is about to be added isn't already in the playlist*/
status_t check_repeat (char * record,char ** playList,int length){
int i;
for (i=0; i < length;i++){
if(strcmp(record, playList[i])==0);
return ST_ALREADY_IN;
}
return OK;
}
/*After receiving a keyword checks any match with all songs in the playlist*/
status_t search_record (char * keyword, char ** playList){
int i,aux=0;
for (i=0; playList[i] != 0 && i < MAX_PLAYLIST; i++){
if(strstr(keyword, playList[i])!= NULL){
printf("%s\n",playList[i]);
aux++;
}
}
if(i==0)
return ST_EMPTY_LIST;
if(aux == 0)
return ST_EMPTY_SEARCH;
printf("%s%s\n",MSG_SEARCH,keyword);
return OK;
}
我很确定问题出在这个函数中:
status_t add_record (char * record,char ** playList){
status_t st;
char aux[90];
strcpy(aux,LIBRARY_DIRECTORY);
strcat(aux,record);
if (!access(aux, F_OK)){
if((st=fill_playlist(record,playList))!=OK)
return st;
}
else{
return ST_WRONG_NAME;
}
return OK;
}
/* once the user inputs a name, and after making some validations adds the song to the playlist(first position that doesn't have a "0" on it)*/
status_t fill_playlist (char * record,char ** playList){
int i;
status_t st;
char * aux;
aux = record;
for(i=0;playList[i] != 0 && i < MAX_PLAYLIST; i++){}
if (i == MAX_PLAYLIST)
return ST_FULL_PLAYLIST;
if((st=check_repeat(aux,playList,i))!=OK)
return st;
playList[i]=aux;
printf("%s\n",MSG_SUCCESFUL_ADD);
return OK;
}
/* Checks the songs which is about to be added isn't already in the playlist*/
status_t check_repeat (char * record,char ** playList,int length){
int i;
for (i=0; i < length;i++){
if(strcmp(record, playList[i])==0);
return ST_ALREADY_IN;
}
return OK;
}
答案 0 :(得分:0)
fill_playlist (song, list)
如果您在到达fill_playlist时查看了回溯,您的电话中会出现问题。您要添加到播放列表中的歌曲不是uniqe指针,而是指向存储在堆栈中的数据。解决这个问题的最快方法是在full_playlist中执行此操作:
将playList[i]=aux;
替换为playList[i]=strdup(aux);
。
我没有阅读其余代码的故障,但那是我能找到的最明显的错误。 (再次释放此字符串缓冲区,将指针strdup()
发送回free()
。