当printf("")时,冒泡排序不起作用;从代码中删除

时间:2015-04-19 11:33:08

标签: c printf

我正在尝试使用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没有给行号,我在这里放置注释//问题来找到printf语句。

1 个答案:

答案 0 :(得分:2)

打开编译器警告

% clang -std=c99 -pedantic -Weverything so29729287.c
so29729287.c:11:6: warning: no previous prototype for function 'swap' [-Wmissing-prototypes]
void swap(struct update_list *a, struct update_list *b){
     ^
so29729287.c:7:11: warning: padding struct 'struct update_list' with 4 bytes to align 'value' [-Wpadded]
    char *value;
          ^
so29729287.c:22:6: warning: no previous prototype for function 'bubbleSort' [-Wmissing-prototypes]
void bubbleSort(struct update_list *start){
     ^
so29729287.c:23:18: warning: unused variable 'i' [-Wunused-variable]
    int swapped, i;
                 ^
so29729287.c:28:13: warning: variable 'ptr1' is uninitialized when used here [-Wuninitialized]
        if (ptr1 == NULL)
            ^~~~
so29729287.c:24:33: note: initialize the variable 'ptr1' to silence this warning
        struct update_list *ptr1;
                                ^
                                 = NULL
so29729287.c:52:5: warning: no previous prototype for function 'number_of_attributes' [-Wmissing-prototypes]
int number_of_attributes(char* db_name, char* r_name)
    ^
so29729287.c:86:5: warning: will never be executed [-Wunreachable-code]
    fclose(fp);
    ^~~~~~
so29729287.c:89:6: warning: no previous prototype for function 'print_list' [-Wmissing-prototypes]
void print_list(struct update_list *start)
     ^
so29729287.c:99:5: warning: no previous prototype for function 'insert' [-Wmissing-prototypes]
int insert(char* db_name, char* rel_name, struct update_list *attribute_values ){
    ^
so29729287.c:100:11: warning: unused variable 'catalog' [-Wunused-variable]
    FILE *catalog, *relation;
          ^
so29729287.c:101:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
so29729287.c:159:9: warning: unused variable 'i' [-Wunused-variable]
    int i;
        ^
so29729287.c:157:14: warning: unused parameter 'argc' [-Wunused-parameter]
int main(int argc, char* argv[]){
             ^
13 warnings generated.

第28行的警告看起来很危险。