简体   繁体   English

Fgets 在每次调用时重写字符串数组位置 [0] 上的最后一行

[英]Fgets rewriting last line on the position [0] of an string array on every call

i'm having a hard time finding the problem on an assignment for college.我很难找到大学作业的问题。 The program is a music player and it has like 5 functions which all work fine, but one, adding songs to the playlist.该程序是一个音乐播放器,它有 5 个功能都可以正常工作,但有一个,将歌曲添加到播放列表中。 The problem is on the second call of the function, when it has to add a second song to the string array playList.问题出在函数的第二次调用上,它必须将第二首歌曲添加到字符串数组 playList 中。 I have noted debugging the code that in the moment I press enter after the fgets() function it changes playList[0] into what i just wrote, without even entering the function which modifies the array.我注意到调试代码,当我在 fgets() 函数之后按下 Enter 键时,它会将 playList[0] 更改为我刚刚编写的内容,甚至没有输入修改数组的函数。 So i think the problem comes from somewhere else than the function.所以我认为问题来自功能以外的其他地方。 I'd use a dynamic array, which would be a lot better but i'm not allowed to use dynamic memory D: (Sorry if i mispelled anything english is not my main language).我会使用动态数组,这会好很多,但我不允许使用动态内存 D:(对不起,如果我拼错了英语不是我的主要语言)。 I believe the problem is in the function fill_playlist, maybe when i assign a string to playList[i] but i can't find where.我相信问题出在函数 fill_playlist 中,也许当我为 playList[i] 分配一个字符串但我找不到在哪里时。 It also happens when i call fgets in the other menu option, Search Record.当我在另一个菜单选项“搜索记录”中调用 fgets 时也会发生这种情况。 I think it's a problem of pointer arithmetic i'm missing.我认为这是我缺少的指针算术问题。 Basically everytime i use fgets() playList[0] is instantly filled with the last string written and i can't fill playList[1] or more.基本上每次我使用 fgets() playList[0] 都会立即填充最后写入的字符串,我无法填充 playList[1] 或更多。
Thanks谢谢
http://pastebin.com/FX2bHUSk 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;
}

I'm pretty sure the problem is in this function:我很确定问题出在这个函数中:

   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;
}

fill_playlist (song, list) The problem in your call you will find if you look at the backtrace when reaching fill_playlist. fill_playlist (song, list)如果你在到达fill_playlist 时查看回溯,你会发现你通话中的问题。 The song you want to add into the playlist is not a uniqe pointer, but points to data stored on the stack.您要添加到播放列表中的歌曲不是唯一指针,而是指向存储在堆栈中的数据。 The quickest way fix this, is to do this inside full_playlist:解决此问题的最快方法是在 full_playlist 中执行此操作:

replace playList[i]=aux;替换playList[i]=aux; with playList[i]=strdup(aux);playList[i]=strdup(aux); . .

I have not read the rest of the code for faults, but that was the most obvious fault I could find.我没有阅读其余的错误代码,但这是我能找到的最明显的错误。 (too free this string buffer again, send the pointer strdup() returned into free() . (再次释放此字符串缓冲区,将返回的指针strdup()发送到free()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM