我正在尝试使用unix文件系统制作dbms。。。
在密码里。。。当我移除102号线时,我的bubbleSort无法正常工作。。。printf语句如何影响下一个语句?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct update_list {
    int attribute_number; //give it in ascending order
    char *value;
    struct update_list *next;
};

void swap(struct update_list *a, struct update_list *b){
    int temp = a->attribute_number;
    char *temp_value;
    temp_value = malloc(strlen(a->value)+1);
    strcpy(temp_value,a->value);
        a->attribute_number = b->attribute_number;
        strcpy(a->value,b->value);
        b->attribute_number = temp;
        strcpy(b->value,temp_value);
    }

void bubbleSort(struct update_list *start){
    int swapped, i;
        struct update_list *ptr1;
        struct update_list *lptr = NULL;

        /* Checking for empty list */
        if (ptr1 == NULL)
        return;

        do
        {
        swapped = 0;
        ptr1 = start;

            while (ptr1->next != lptr)
            {
                if (ptr1->attribute_number > ptr1->next->attribute_number)
                {
                    swap(ptr1, ptr1->next);
                    swapped = 1;
                    }
                ptr1 = ptr1->next;
            }
        lptr = ptr1;
            }
        while (swapped);
    }



int number_of_attributes(char* db_name, char* r_name)
{
    FILE *fp;
    char buff[50], fname[50];
    int count = 0;
    strcpy(fname,"_r");
    strcat(fname,r_name);

    char catalog_address[100];
    strcpy(catalog_address, db_name );
    strcat(catalog_address,"/_catalog_");
    fp = fopen( catalog_address, "r" );
    if (1)
    //fscanf(fp, "%s", buff);
    while (!feof(fp))
    {
        fscanf(fp, "%s", buff);
        if (strcmp ( buff, fname)==0)
        {
            fscanf(fp, "%s", buff);
            while((strcmp(buff,"__")!=0) && !feof(fp))
            {
                count++;
                fscanf(fp, "%s", buff);
                if (!feof(fp)) fscanf(fp, "%s", buff);

            }
            return (count);
        }
        /*while(strcmp(buff,"_")!=0)
            fscanf(fp, "%s", buff);*/
        //fscanf(fp, "%s", buff);
    }
    return (-1);
    fclose(fp);
}

void print_list(struct update_list *start)
{
    struct update_list *temp = start;
    printf("\n");
    while (temp!=NULL)
    {
    printf("%d ", temp->attribute_number);
    temp = temp->next;
    }
}
int insert(char* db_name, char* rel_name, struct update_list *attribute_values ){
    FILE *catalog, *relation;
    int i;
    printf(" "); //problem here ...I cannot remove this
    bubbleSort(attribute_values);
    print_list(attribute_values);
    char rel_address[100];

    strcpy(rel_address, db_name );
    strcat(rel_address,"/");
    strcat(rel_address,rel_name);
    relation = fopen(rel_address, "r");

    //take the first element of att_list

    char *PK;
    PK = malloc(strlen(attribute_values->value)+1);
    strcpy(PK,attribute_values->value);
    strcat(PK,"\n");

    //get num_of_attr = t
    int t = number_of_attributes(db_name,rel_name);
    //after every t lines, check value = value of first element
    const size_t line_size = 300;
    char* line = malloc(line_size);
    int count = 0;
    while (fgets(line, line_size, relation) != NULL){
        if(count%t == 0){
            if(strcmp(line, PK) == 0){
                free(PK);
                return -2;
                }
            }
        count++;
    }
    free(line);
    //else append at the end: each value of attr_list
    struct update_list *current;
    current = attribute_values;
    char shellscript[10000];
    strcpy(shellscript,"\n");
    while(current != NULL){
        strcat(shellscript,"echo ");
        strcat(shellscript,current->value);
        strcat(shellscript,">> ");
        strcat(shellscript,rel_address);

        strcat(shellscript,"\n");

        current = current->next;
    }

    system(shellscript);
    free(PK);

    return 0;

}
int main(int argc, char* argv[]){

    int i;
    struct update_list *head,*current;

    head = (struct update_list*)malloc(sizeof(struct update_list));
    current = (struct update_list*)malloc(sizeof(struct update_list));

    head->attribute_number = 3;
    head->value = malloc(100);
    strcpy(head->value, "kruthi");

    head->next = current;

    current->attribute_number = 2;
    current->value = malloc(100);
    strcpy(current->value,"kakjd");
    current->next=NULL;

    //bubbleSort(head);
    insert(argv[1], argv[2], head);
    free(head->value);
    free(current->value);
    free(head);
    free(current);

    return 0;
}

bubbleSort在主功能中运行得很好。。。
编辑:我在insert函数中调用了bubbleSort,因为stackoverflow没有给出行号,所以我在这里放置了comment//the problem来定位printf语句。

最佳答案

打开编译器警告

% clang -std=c99 -pedantic -Weverything so29729287.c

so29729287.c:11:6:警告:没有以前的函数“swap”原型[-Wmissing prototypes]
void swap(结构更新列表*a,结构更新列表*b){
^
so29729287.c:7:11:警告:用4个字节填充结构“struct update_list”以对齐“value”[-Wpadded]
字符*值;
^
so29729287.c:22:6:警告:没有“bubbleSort”函数的先前原型[-Wmissing prototypes]
void bubbleSort(结构更新列表*开始){
^
so29729287.c:23:18:警告:未使用的变量“i”[-Wunused variable]
智力交换,我;
^
so29729287.c:28:13:警告:此处使用变量“ptr1”时未初始化[-Wuninitialized]
如果(ptr1==NULL)
^~~~
so29729287.c:24:33:注意:初始化变量“ptr1”以消除此警告
结构更新列表*ptr1;
^
=空
so29729287.c:52:5:警告:函数“number_of_attributes”没有以前的原型。[-Wmissing prototypes]
int number_of_属性(char*db_name,char*r_name)
^
so29729287.c:86:5:警告:永远不会执行[-Wunreachable代码]
fclose(fp);
^~~~~~
so29729287.c:89:6:警告:功能“print_list”没有以前的原型。[-Wmissing prototypes]
无效打印列表(结构更新列表*开始)
^
so29729287.c:99:5:警告:没有以前的函数“insert”原型[-Wmissing prototypes]
int插入(char*db_name,char*rel_name,struct update_list*attribute_values){
^
so29729287.c:100:11:警告:未使用的变量“catalog”[-Wunused variable]
文件*目录,*关系;
^
so29729287.c:101:9:警告:未使用的变量“i”[-Wunused variable]
内景一;
^
so29729287.c:159:9:警告:未使用的变量“i”[-Wunused variable]
内景一;
^
so29729287.c:157:14:警告:未使用的参数“argc”[-Wunused parameter]
int main(int argc,char*argv[]){
^
生成13个警告。
28号线的警告看起来很危险。

关于c - 当printf(“”);时冒泡排序不起作用;从代码中删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29729287/

10-11 16:41