Hi, I am trying to read the following information from a text file and save it a binary search tree.
The data are:

Toyota 1.3                    Solid             	33,235.04	            3300.00
Nissan1.3	                     Solid 	                38,235.04	            3300.00
struct Node
{
    char model[50];
    char colourType[20];
    double price;
    double deposit;
    struct Node *parent;
    struct Node *left;
    struct Node *right;
};

This is my code,

void load_file()
{
    char temp[2000];
    
    char temp1[50];
    char temp2[20];
    double temp3;
    double temp4;
    struct Node *Root = NULL;

    FILE*fp=fopen("class.txt", "r");

    while( (fscanf(fp, "%s%s%lf%lf", temp1, temp2, &temp3, temp4))!=EOF)
    {
        struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
        strcpy(newNode->model, temp1); // newNode->model = temp1;
        strcpy(newNode->colourType, temp2); // newNode->colourType = temp2;
        newNode->price = temp3;
        newNode->deposit = temp4;

        newNode->parent=NULL;
        newNode->left=NULL;
        newNode->right=NULL;
        Root=Insert(Root,newNode);
    }
    PrintTree(Root);
    return;
}

My problem is when i print out the data in my tree, the data are not print out as the sequence in the text file.
Please help me, thank you.

cjjack88

Edited 7 Years Ago by cjjack88: n/a

There are two unclear functions, Insert and printTree. I think you should check them carefully. It seems the problem be there.

Hi, I am trying to read the following information from a text file and save it a binary search tree.
The data are:

Toyota 1.3                    Solid             	33,235.04	            3300.00
Nissan1.3	                     Solid 	                38,235.04	            3300.00
struct Node
{
    char model[50];
    char colourType[20];
    double price;
    double deposit;
    struct Node *parent;
    struct Node *left;
    struct Node *right;
};

This is my code,

void load_file()
{
    char temp[2000];
    
    char temp1[50];
    char temp2[20];
    double temp3;
    double temp4;
    struct Node *Root = NULL;

    FILE*fp=fopen("class.txt", "r");

    while( (fscanf(fp, "%s%s%lf%lf", temp1, temp2, &temp3, temp4))!=EOF)
    {
        struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));
        strcpy(newNode->model, temp1); // newNode->model = temp1;
        strcpy(newNode->colourType, temp2); // newNode->colourType = temp2;
        newNode->price = temp3;
        newNode->deposit = temp4;

        newNode->parent=NULL;
        newNode->left=NULL;
        newNode->right=NULL;
        Root=Insert(Root,newNode);
    }
    PrintTree(Root);
    return;
}

My problem is when i print out the data in my tree, the data are not print out as the sequence in the text file.
Please help me, thank you.

cjjack88

There are two unclear functions, Insert and printTree. I think you should check them carefully. It seems the problem be there.

Thank you for reply, my insert and printTree should be no problem because when i run my program without read data from file, my program can run. The main problem is when i read a String "Nissan sentra" from text file, it will read it as 2 strings and my output become like this:

Model                    colour type              price               deposit
Nissan                    sentra                    000000           0000000
metalic                    metalic                  450000.00       320000.00

My insert function:

struct Node *Insert(struct Node *Root,struct Node *newNode)
{
    struct Node *back=(struct Node*)malloc(sizeof(struct Node));
    struct Node *pointer=(struct Node*)malloc(sizeof(struct Node));
    back=NULL;
    pointer=Root;

    while(pointer!=NULL)
    {
        back=pointer;
        if(strcmp( newNode->model, pointer->model) < 0) //newNode->key<pointer->key
            pointer=pointer->left;
        else
            pointer=pointer->right;
    }

    newNode->parent=back;

    if( back == NULL )
        Root = newNode;
    else
    {
        if( strcmp ( newNode->model, back->model ) < 0 ) //newNode->key<back->key
            back->left = newNode;
        else
            back->right = newNode;
    }

    return Root;
}

My printTree Function:

void PrintTree(struct Node *Root)
{
    FILE *out;

    out=fopen("class.txt","a");

    struct Node *pointer = (struct Node*)malloc(sizeof(struct Node));
    pointer=Root;
    if(pointer!=NULL)
    {
        PrintTree(pointer->left);
        printf("%-25s %-10s %15.2lf %15.2lf\n", pointer->model, pointer->colourType, pointer->price, pointer->deposit);
        fprintf(out, "%s\t%s \t%.2lf\t%.2lf\n", pointer->model, pointer->colourType, pointer->price, pointer->deposit);
        PrintTree(pointer->right);
    }
     fclose(out);
}

Edited 7 Years Ago by cjjack88: n/a

you are reading the file in wrong order

here is a sample

void read()
{
     FILE *fp;
     int x;
     char t1[50],t2[50];
     float f1,f2;
     fp=fopen("read.txt","r");
     if(fp)
     {
     while((x=fscanf(fp,"%s%f%s%f",t1,&f1,t2,&f2))!=EOF)
     {  printf("x=%d\n",x);
        printf("%s\n %f\n %s\n %f\n",t1,f1,t2,f2);
       getchar();
     }
     printf("END x=%d\n",x);
     }
     else
        printf("File open failed\n");
     fclose(fp);
}
Attachments
Sirisha 13  Dismey 7
Udaya 18    mamatha  20
Raghavendra 22    Naresh 23

I think you encounter the problem whwn you try to read a two word phrase as a single string. If it's true, it is cause of scanf native. scanf stop scanning a particular string field before it reaches the normal end-of-field or whitespace character, or it might terminate entirely.
In your example there is a whitespace character between Nissan and sentra.
scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.
The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.
However I'm not a C professional, I hope I sugested true.

Comments
yes

I think you encounter the problem whwn you try to read a two word phrase as a single string. If it's true, it is cause of scanf native. scanf stop scanning a particular string field before it reaches the normal end-of-field or whitespace character, or it might terminate entirely.
In your example there is a whitespace character between Nissan and sentra.
scanf often leads to unexpected results if you diverge from an expected pattern. You must provide information that tells scanf how to synchronize at the end of a line.
The combination of gets or fgets followed by sscanf is safe and easy, and therefore recommended over scanf.
However I'm not a C professional, I hope I sugested true.

Yes, this is the problem, if i enter nissanSentra, my program will run correctly, if i enter in normal form "nissan sentra", my data will loss sequence.
Can u show the example code how to do the combination of gets or fgets followed by sscanf , i can not find it in book.
Thank you.

you are reading the file in wrong order

here is a sample

void read()
{
     FILE *fp;
     int x;
     char t1[50],t2[50];
     float f1,f2;
     fp=fopen("read.txt","r");
     if(fp)
     {
     while((x=fscanf(fp,"%s%f%s%f",t1,&f1,t2,&f2))!=EOF)
     {  printf("x=%d\n",x);
        printf("%s\n %f\n %s\n %f\n",t1,f1,t2,f2);
       getchar();
     }
     printf("END x=%d\n",x);
     }
     else
        printf("File open failed\n");
     fclose(fp);
}

Thank you for the example, i will try it.

This article has been dead for over six months. Start a new discussion instead.