C 奇怪的字符替换(对我来说)毫无意义

C 奇怪的字符替换(对我来说)毫无意义,c,string,parsing,strsep,C,String,Parsing,Strsep,很抱歉,代码太长了,但是我被难住了,需要帮助 我的具体问题是,当我使用parse方法调用一个“del”事件时,我会在一行中进行奇怪的字符替换,据我所知,这行代码没有被触动。当我直接调用函数“del”时,这种情况不会发生。我花了太长时间研究这段代码,但毫无用处,试图实现一种不同的字符串标记方法;你说吧。我包含了所有代码,因为我不知道问题在哪里,因为我开始认为它不在函数“parse”中 我会留下来,根据需要提供尽可能多的信息,因为现在我不知道还能补充什么 另外,编译器链接到gnu windows库,

很抱歉,代码太长了,但是我被难住了,需要帮助

我的具体问题是,当我使用parse方法调用一个“del”事件时,我会在一行中进行奇怪的字符替换,据我所知,这行代码没有被触动。当我直接调用函数“del”时,这种情况不会发生。我花了太长时间研究这段代码,但毫无用处,试图实现一种不同的字符串标记方法;你说吧。我包含了所有代码,因为我不知道问题在哪里,因为我开始认为它不在函数“parse”中

我会留下来,根据需要提供尽可能多的信息,因为现在我不知道还能补充什么

另外,编译器链接到gnu windows库,这就是strep的来源

p.p.S.唯一的编译器警告是它抱怨我没有使用char*buf

db.h

db.c

#包括
#包括
#包括
#包括
#包括
#包括
#包括“db.h”
int isLong(字符*str){
如果(str==NULL){
返回0;
}
char*pEnd;
strtol(str和pEnd,10);
如果(isalpha(*pEnd)| |*pEnd==''){
返回0;
}否则{
返回1;
}
}
int-isFloat(字符*str){
如果(str==NULL){
返回0;
}
char*pEnd;
strtod(str和pEnd);
如果(isalpha(*pEnd)| |*pEnd==''){
返回0;
}否则{
返回1;
}
}
无效添加(项目,长id,字符*名称,字符*描述,浮动价格){
int i=0;
而(1){
if(项[i].id==id){
printf(“ID为%lu的项目\%s\”已存在。\n”,项目[i]。名称,ID);
打破
}else if(项[i].id==0){
项目[i].id=id;
项目[i]。名称=名称;
项目[i]。描述=描述;
第[i]项。价格=价格;
打破
}否则{
i++;
}
}
}
void del(项目_t*项目,长id){
int i=0;
int end=FindLasteElement(项目);
而(1){
if(项[i].id==id){
项目[i]。id=0;
第[i]项。名称=”;
第[i]项。desc=“”;
项目[i]。价格=0;
while(iid=从->id;
to->name=from->name;
to->desc=从->描述;
到->价格=从->价格;
}
void解析(item_t*item,char*str){
字符**ap,*argv[10],*inputstr=malloc(sizeof(str)),*ptr;
strcpy(输入str,str);
memset(argv,0,sizeof(argv));
对于(ap=argv;(*ap=strep(&inputstr,“,\n”))!=NULL;){
如果(**ap!='\0'){
如果(++ap>=&argv[10]){
打破
}
}
}
if(strcmp(argv[0],“add\0”)==0){
如果(!isLong(argv[1])| | argv[1]==NULL){
printf(“\n无效/缺少ID\n”);
返回;
}else if(argv[2]==NULL){
printf(“\n无效/缺少产品名称。\n”);
返回;
}else if(argv[3]==NULL){
printf(“\n无效/缺少产品说明。\n”);
返回;
}如果(!isFloat(argv[4])| | argv[4]==NULL,则为else){
printf(“\n无效/缺少价格\n”);
返回;
}否则{
添加(项目,strtol(argv[1],&ptr,10),argv[2],argv[3],strtod(argv[4],&ptr));
}
}else if(strcmp(argv[0],“del\0”)==0){
如果(!isLong(argv[1])| | argv[1]==NULL){
printf(“\n无效/缺少ID\n”);
返回;
}否则{
del(项目,标准(argv[1],&ptr,10));
}
}
} 
int FindLasteElement(项目_t*项目){
对于(int i=0;i
main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "db.h"


int main (int argc, char* argv[]) {
    item_t item [MAX_ITEMS];
    memset (item, 0, sizeof (item));
    char* buf = malloc (255);
    int exit = 0;

    while (!exit) {
        printf ("Adding 3 test items.\n");
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        add (item, 5678, "Pasta", "Tasty Pasta", 19.99F);
        add (item, 9012, "Ribs", "Tasty Ribs", 29.99F);
        disp (item, -1);
        printf ("\nDeleting Item with ID 5678.\n");
        del (item, 5678);
        disp (item, -1);
        printf ("\nModifying item with ID 1234.\n");
        modify (item, 1234, "Soup", "Tasty Soup", 4.99F);
        disp (item, -1);
        printf ("\nAdding another item with id 5678.");
        add (item, 5678, "Pasta", "Tasty Pasta", 19.99F);
        printf ("\nAdding item \"Pizza\" with the same ID as \"Soup\".\n");
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        disp (item, -1);
        printf ("\nDeleting item \"Soup\" and re-adding \"Pizza\".\n");
        del (item, 1234);
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        disp (item, -1);
        printf ("\nDisplaying only the item with id 5678.\n");
        disp (item, 5678);
        printf ("\nAdding item \"Wings\" using the parsing function.\n");
        parse (item, "add,9898,Wings,Tasty Wings,14.99\n");
        disp (item, -1);
        printf ("\nAttempting to Delete non-existent item with ID 9999.\n");
        //del (item, 9999);
        parse (item, "del,9999\n"); <--- This is the problem line
        printf ("\nAttempting to Modify non-existent item with ID 9999.\n");
        modify (item, 9999, "Test", "Test", 0.0F);
        disp (item, -1);
        exit = 1;
    }
}
#包括
#包括
#包括
#包括
#包括
#包括“db.h”
int main(int argc,char*argv[]){
项目[最大项目];
memset(项目,0,大小(项目));
char*buf=malloc(255);
int exit=0;
当(!退出){
printf(“添加3个测试项目。\n”);
添加(项目1234,“比萨饼”,“美味比萨饼”,9.99F);
添加(第5678项,“意大利面”、“美味意大利面”,19.99F);
添加(第9012项,“排骨”,“美味排骨”,29.99F);
显示(项目-1);
printf(“\n删除ID为5678的项。\n”);
删除(项目5678);
显示(项目-1);
printf(“\n修改ID为1234的项。\n”);
修改(项目1234,“汤”,“美味汤”,4.99F);
显示(项目-1);
printf(“\n添加另一个id为5678的项目”);
添加(第5678项,“意大利面”、“美味意大利面”,
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "db.h"

int isLong (char* str) {
    if (str == NULL) {
        return 0;
    }
    char* pEnd;
    strtol (str, &pEnd, 10);
    if (isalpha (*pEnd) || *pEnd == ' ') {
        return 0;
    } else {
        return 1;
    }
}

int isFloat (char* str) {
    if (str == NULL) {
        return 0;
    }
    char* pEnd;
    strtod (str, &pEnd);
    if (isalpha (*pEnd) || *pEnd == ' ') {
        return 0;
    } else {
        return 1;
    }
}


void add (item_t *item, long id, char* name, char* desc, float price) {
    int i = 0;
    while (1) {
        if (item[i].id == id) {
            printf ("Item \"%s\" with ID %lu already exists.\n", item[i].name, id);
            break;
        } else if (item[i].id == 0) {
            item[i].id = id;
            item[i].name = name;
            item[i].desc = desc;
            item[i].price = price;
            break;
        } else {
            i++;
        }
    }
}

void del (item_t* item, long id) {
    int i = 0;
    int end = findLastElement (item);
    while (1) {
        if (item[i].id == id) {
            item[i].id = 0;
            item[i].name = "";
            item[i].desc = "";
            item[i].price = 0;
            while (i < end) {
                itemCopy (&item [i + 1], &item [i]);
                i++;
            }
            break;
        } else {
            if (i == MAX_ITEMS) {
                printf ("Item with ID %lu does not exist.\n", id);
                break;
            }
            i++;
        }
    }
}

void modify (item_t *item, long id, char* name, char* desc, float price) {
    int i = 0;
    while (1) {
        if (item[i].id == id) {
            item[i].name = name;
            item[i].desc = desc;
            item[i].price = price;
            break;
        } else {
            if (i == MAX_ITEMS) {
                printf ("Item with ID %lu does not exist.\n", id);
                break;
            }
            i++;
        }
    }
}

void disp (item_t* item, long id) {
    int end = findLastElement (item);
    printf ("\nID\tNAME\tDESCRIPTION\tPRICE\n--\t----\t-----------\t-----\n");
    if (id == -1) {
        for (int i = 0; i < end; i++) {
            printf ("%lu\t%s\t%s\t$%2.2f\n", item[i].id, item[i].name, item[i].desc, item[i].price);
        }
    } else {
        for (int i = 0; i < end; i++) {
            if (item[i].id == id) {
                printf ("%lu\t%s\t%s\t$%2.2f\n", item[i].id, item[i].name, item[i].desc, item[i].price);
                break;
            }
        }
    }
}

void itemCopy (item_t* from, item_t* to) {
    to -> id = from -> id;
    to -> name = from -> name;
    to -> desc = from -> desc;
    to -> price = from -> price;
}

void parse (item_t* item, char* str) {
    char **ap, *argv[10], *inputstr = malloc (sizeof(str)), *ptr;
    strcpy (inputstr, str);
    memset (argv, 0, sizeof (argv));
    for (ap = argv; (*ap = strsep (&inputstr, ",\n")) != NULL;) {
        if (**ap != '\0') {
            if (++ap >= &argv[10]) {
                break;
            }
        }
    }

    if (strcmp (argv[0], "add\0") == 0) {
        if (!isLong (argv[1]) || argv[1] == NULL) {
            printf ("\nInvalid/Missing ID\n");
            return;
        } else if (argv[2] == NULL) {
            printf ("\nInvalid/Missing Product Name.\n");
            return;
        } else if (argv[3] == NULL) {
            printf ("\nInvalid/Missing Product Description.\n");
            return;
        } else if (!isFloat (argv[4]) || argv[4] == NULL) {
            printf ("\nInvalid/Missing Price\n");
            return;
        } else {
            add (item, strtol (argv[1], &ptr, 10), argv[2], argv[3], strtod (argv[4], &ptr));
        }
    } else if (strcmp (argv[0], "del\0") == 0) {
        if (!isLong (argv[1]) || argv[1] == NULL) {
            printf ("\nInvalid/Missing ID\n");
            return;
        } else {
            del (item, strtol (argv[1], &ptr, 10));
        }
    }
} 

int findLastElement (item_t* item) {
    for (int i = 0; i < MAX_ITEMS; i++) {
        if (item[i].id == 0) return i;
    }
    return -1;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "db.h"


int main (int argc, char* argv[]) {
    item_t item [MAX_ITEMS];
    memset (item, 0, sizeof (item));
    char* buf = malloc (255);
    int exit = 0;

    while (!exit) {
        printf ("Adding 3 test items.\n");
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        add (item, 5678, "Pasta", "Tasty Pasta", 19.99F);
        add (item, 9012, "Ribs", "Tasty Ribs", 29.99F);
        disp (item, -1);
        printf ("\nDeleting Item with ID 5678.\n");
        del (item, 5678);
        disp (item, -1);
        printf ("\nModifying item with ID 1234.\n");
        modify (item, 1234, "Soup", "Tasty Soup", 4.99F);
        disp (item, -1);
        printf ("\nAdding another item with id 5678.");
        add (item, 5678, "Pasta", "Tasty Pasta", 19.99F);
        printf ("\nAdding item \"Pizza\" with the same ID as \"Soup\".\n");
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        disp (item, -1);
        printf ("\nDeleting item \"Soup\" and re-adding \"Pizza\".\n");
        del (item, 1234);
        add (item, 1234, "Pizza", "Tasty Pizza", 9.99F);
        disp (item, -1);
        printf ("\nDisplaying only the item with id 5678.\n");
        disp (item, 5678);
        printf ("\nAdding item \"Wings\" using the parsing function.\n");
        parse (item, "add,9898,Wings,Tasty Wings,14.99\n");
        disp (item, -1);
        printf ("\nAttempting to Delete non-existent item with ID 9999.\n");
        //del (item, 9999);
        parse (item, "del,9999\n"); <--- This is the problem line
        printf ("\nAttempting to Modify non-existent item with ID 9999.\n");
        modify (item, 9999, "Test", "Test", 0.0F);
        disp (item, -1);
        exit = 1;
    }
}
Adding 3 test items.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
1234    Pizza   Tasty Pizza     $9.99
5678    Pasta   Tasty Pasta     $19.99
9012    Ribs    Tasty Ribs      $29.99

Deleting Item with ID 5678.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
1234    Pizza   Tasty Pizza     $9.99
9012    Ribs    Tasty Ribs      $29.99

Modifying item with ID 1234.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
1234    Soup    Tasty Soup      $4.99
9012    Ribs    Tasty Ribs      $29.99

Adding another item with id 5678.
Adding item "Pizza" with the same ID as "Soup".
Item "Soup" with ID 1234 already exists.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
1234    Soup    Tasty Soup      $4.99
9012    Ribs    Tasty Ribs      $29.99
5678    Pasta   Tasty Pasta     $19.99

Deleting item "Soup" and re-adding "Pizza".

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
9012    Ribs    Tasty Ribs      $29.99
5678    Pasta   Tasty Pasta     $19.99
1234    Pizza   Tasty Pizza     $9.99

Displaying only the item with id 5678.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
5678    Pasta   Tasty Pasta     $19.99

Adding item "Wings" using the parsing function.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
9012    Ribs    Tasty Ribs      $29.99
5678    Pasta   Tasty Pasta     $19.99
1234    Pizza   Tasty Pizza     $9.99
9898    Wings   Tasty Wings     $14.99

Attempting to Delete non-existent item with ID 9999.
Item with ID 9999 does not exist.

Attempting to Modify non-existent item with ID 9999.
Item with ID 9999 does not exist.

ID      NAME    DESCRIPTION     PRICE
--      ----    -----------     -----
9012    Ribs    Tasty Ribs      $29.99
5678    Pasta   Tasty Pasta     $19.99
1234    Pizza   Tasty Pizza     $9.99
9898    Win♦    Çdel    $14.99 <--- THIS IS WHAT'S DOING MY HEAD IN
 inputstr = malloc (sizeof(str));
 strcpy(inputstr, str);
 inputstr = strdup(str);
char* inputstr = malloc(strlen(str) + 1);
strcpy(inputstr, str);