c - 冒泡排序在 printf (""时不起作用;从代码中删除

标签 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 语句。

最佳答案

打开你的编译器警告

% 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 行的警告看起来很危险。

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

相关文章:

从打印更改为写入文件 c

c - 使用 xml 解析器 expat 获取 xml 数据

无法理解此代码的输出

c++ - 我应该使用什么数据结构来支持插入、删除和随机选择?

c - 在 C 中迭代 CSV 文件中的列

c - char* 的位运算

bash - bash 中的 printf "-1"给出错误,原因未知

C 程序 - printf 与其他 printf 字符重叠

c - 如何使用两个枚举和一个 const char* 根据文本结果打印颜色?

c++ - 微软 _stprintf 警告