I am trying to make a dbms using unix file system ... In the code ... when I remove line 102 my bubbleSort doesnt work properly ... How does a printf statement affect immediately next statement?
#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 was working perfectly fine when kept in main function ...
Edit: I have called bubbleSort inside insert function, since stackoverflow doesnt give line numbers, I have put comment //the problem here to locate the printf statement.
Aucun commentaire:
Enregistrer un commentaire