• C

Unable to read from the file

Hi,

Here is my code. It is quite long. However please look at the line 199 which is marked as line no 199 and is left aligned in the code. I am unable to read the values from the file data_current.txt. Also see line no. 124 where I have written the values into the file data_current.txt.

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}
void setnum(void);
int getnum(void);
//Two different structures have been used bcos data in data_cur.txt needs
//only two data - name and cur_exp.
//whereas data.txt needs all the data which is again used for final calculation
struct account_cur
{
 char *name;
 float cur_exp;
 };
struct account_data
{
  struct account_cur  *base_class;
  float cur_exp,prev_debit , prev_credit, perhead_exp ;
 struct account_data *next;
 };

typedef struct account_data acc_data;
//typedef struct account_cur acc_cur;
display(acc_data **);
void add(acc_data **);
calculate(acc_data *);
int main()
{
int i;
clrscr();
acc_data *list_acc_data;

clrscr();
//Print the various options available to the user
printf("1. Add records\n");
printf("2. View records\n");
printf("3. Calculate\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
clrscr();
switch(i)
 {
   case 1:
   add(&list_acc_data);
         break;
   case 2: clrscr();
   display(&list_acc_data);
         break;
   case 3: calculate(list_acc_data);
         break;
   case 4:
   case 5:
   case 6: setnum();
         break;
   default:
   printf("Input data unknown\n");
  };
getch();
return(0);
}
display(acc_data **list_ptr)
 {
  FILE *fp_cur,*fp_data;
  int i,num;
  acc_data *temp,*new_blk;
  temp = *list_ptr;
  fp_data = fopen("c:\\tcplus\\tc\\data.txt", "r");
  //if (fp_cur==NULL)
   //printf("Unable to open file data_current.txt");
  if (fp_data == NULL)
   printf("Unable to open file data.txt");
  num = getnum();
  for(i=0;i<=num;i++)
   {
   new_blk = (acc_data *)malloc(sizeof(acc_data));
   fscanf(fp_data,"%s %f %f\n", new_blk->base_class->name, new_blk->prev_debit, new_blk->prev_credit);
   new_blk->next == NULL;
   if (*list_ptr == NULL)
     {
       temp = new_blk;
       *list_ptr = temp;
     }
    else
     {
      temp->next = new_blk;
      temp = temp->next;
     }
   }
  fclose(fp_data);
   getch();

/*    while(list_ptr != NULL)

    {
      printf("\n %s" ,list_ptr->base_class->name);
      list_ptr  = list_ptr->next;
     }*/

  }
 void add(acc_data **list_ptr)
 {
   FILE *fp_cur;
   acc_data *temp, *new_blk;
   temp = *list_ptr;
   int i,number;
   number = getnum();
   //Point to ponder: Everytime when cur_exp is input the previous data should be overwritten
   //Hence the file is opened in write mode.
   fp_cur = fopen("c:\\tcplus\\tc\\data_current.txt","w");
   if(fp_cur==NULL)
   printf("Error opening file\n");
   for(i=1;i<=number;i++)
   {
        new_blk = (acc_data *)malloc(sizeof(acc_data));
        printf("\nEnter the name of person %d", i);
        new_blk->base_class->name = (char *)malloc(30); //allocate memory to name
        scanf("%s",new_blk->base_class->name);  //This part gets written correctly

        printf("\n Enter the current expenditure of %s", new_blk->base_class->name);
        scanf("%f",&new_blk->base_class->cur_exp);
 /* line no. 124 */        fprintf(fp_cur,"%s %f\n ",new_blk->base_class->name, new_blk->base_class->cur_exp);
        new_blk->next = NULL;
        if(*list_ptr == NULL)
         {
         temp = new_blk;
         *list_ptr = temp;
        }
        else
         {
         temp->next = new_blk;
         temp = temp->next;
       }

   }
   fclose(fp_cur);
   flcose(fp_data);
   }
   // the following lines were added to compensate for the disinclusion of
   //the floating point library by the borland or tc complier.
   //Refer documentation for more details
   static void forcefloat(float *p)
    {
     float f = *p;
     forcefloat(&f);
     }

   void setnum()
     {
       FILE *fp_stat;
       int num;
       fp_stat = fopen("c:\\tcplus\\tc\\statistics.txt","w");
       if(fp_stat ==NULL)
       printf("Unable to open file statistics.txt");
       else
      {
       printf("Enter the number of people in the house");
       scanf("%d",&num);
       fprintf(fp_stat,"Number of people in the house = %d",num);
       fprintf(stdout,"The number of people in the house is now set to %d", num);
       printf("\n press any key to continue...");
       getch();
      }
    }

    int getnum()
     {
       FILE *fp_stat;
       int num;
       fp_stat = fopen("c:\\tcplus\\tc\\statistics.txt","r");
       if(fp_stat ==NULL)
       printf("Unable to open file statistics.txt");
       else
      {
        fscanf(fp_stat,"Number of people in the house = %d",&num);
        fprintf(stdout,"Number of people in the house = %d",num);
      }
      return(num);
       }
     calculate(acc_data *list_ptr)
      {
       acc_data *temp,*new_blk;
       FILE *fp_cur, *fp_data;
       int number,i;
       number = getnum();
       float total = 0, average,debit, credit,curexp;
       char *name;
       temp = list_ptr;
       fp_cur  = fopen("c:\\tcplus\\tc\\data_current.txt","r+");
       fp_data = fopen("c:\\tcplus\\tc\\data.txt","r+");
       if(fp_cur ==NULL || fp_data ==NULL)
         printf("\nUnable to open the required files for calucation");
       else
        {
         for(i = 1; i<=number; i++)
         {
          new_blk = (acc_data *) malloc(sizeof(acc_data));
/*line no.199*/          fscanf(fp_cur,"%s %f ", new_blk->base_class->name, &new_blk->base_class->cur_exp);   //Unable to get the data from the file data_current.txt
          fscanf(fp_data,"%f %f",new_blk->prev_debit,new_blk->prev_credit);
          total = total + new_blk->base_class->cur_exp;
           if(list_ptr == NULL)
            {
            temp = new_blk;
            new_blk->next = NULL;
            }
           else
            {
            temp->next = new_blk;
            temp = temp->next;
             }

           }
           fclose(fp_data);

        average = total/5;
        temp = list_ptr;
        fp_data = fopen("c:\\tcplus\\tc\\data.txt","r+");
        for(i=1;i<=number;i++)
         {
           while(temp->next != NULL)
             {
             if(temp->base_class->cur_exp == average)
                {
                 temp->prev_debit = 0.0;
                 temp->prev_credit = 0.0;
 /*line no. 224 */                 fprintf(fp_data,"%s %2f %2f",temp->base_class->name, temp->prev_debit, temp->prev_credit);//error: Illegal use of floating point
                 }
               else if(temp->base_class->cur_exp > average)
                 {
                  debit = 0;
                  credit = credit + (temp->base_class->cur_exp - average);
                  temp->prev_credit = credit;
/*line no. 231*/                  fprintf(fp_data, "%s %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                   }
                else
                 {
                  credit = 0;
                  debit = debit + (average-temp->base_class->cur_exp);
                  temp->prev_debit = debit;
/*line no. 238*/                  fprintf(fp_data, "%d %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                  }
                }
              }
             }
            }

Thnx in advance.
ksanand_beAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
in your display() function

 >>  fscanf(fp_data,"%s %f %f\n", new_blk->base_class->name, new_blk->prev_debit, new_blk->prev_credit);

this shall cause error. you should do

new_blk = (acc_data *)malloc(sizeof(acc_data));
new_blk->base_class = (struct account_cur *)malloc(sizeof(struct account_cur));
char somename[1000];
then,
fscanf(fp_data,"%s %f %f\n", somename, new_blk->prev_debit, new_blk->prev_credit);
new_blk->base_class->name = strdup(somename);

hope this helps,
varun
0
van_dyCommented:
thre are quite a few more problems with the code

in the function display() itself

 >>new_blk = (acc_data *)malloc(sizeof(acc_data));
 >>  fscanf(fp_data,"%s %f %f\n", new_blk->base_class->name, new_blk->prev_debit, new_blk->prev_credit);
 >>  new_blk->next == NULL;   <--------- dont you want new_blk->next = NULL; ??

>>if (*list_ptr == NULL)         //*list_ptr isnt gauranteed to be null the first time through the loop.
>>     {
 >>      temp = new_blk;
 >>      *list_ptr = temp;
 >>    }

may be in main you want to do ..
nt main()
{
int i;
clrscr();
acc_data *list_acc_data = NULL; //changed
...//rest of main

0
van_dyCommented:
similarily in calculate();


>>new_blk = (acc_data *) malloc(sizeof(acc_data));
>>/*line no.199*/         fscanf(fp_cur,"%s %f ", new_blk->base_class->name, &new_blk->base_class->cur_exp);  

do
new_blk = (acc_data *)malloc(sizeof(acc_data));
new_blk->base_class = (struct account_cur *)malloc(sizeof(struct account_cur));
char somename[1000];
/*line no.199*/         fscanf(fp_cur,"%s %f ", somename, &new_blk->base_class->cur_exp);  
new_blk->base_class->name = strdup(somename);




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

ksanand_beAuthor Commented:
The result is still the same. It shows junk values when I tired to watch the variables. Also I'm getting an indefinite loop in the following section of the code.

/*Indefinite loop */           while(temp->next != NULL)
             {
             if(temp->base_class->cur_exp == average)
                {
                 temp->prev_debit = 0.0;
                 temp->prev_credit = 0.0;
 /*line no. 224 */                 fprintf(fp_data,"%s %2f %2f",temp->base_class->name, temp->prev_debit, temp->prev_credit);//error: Illegal use of floating point
                 }
               else if(temp->base_class->cur_exp > average)
                 {
                  debit = 0;
                  credit = credit + (temp->base_class->cur_exp - average);
                  temp->prev_credit = credit;
/*line no. 231*/                  fprintf(fp_data, "%s %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                   }
                else
                 {
                  credit = 0;
                  debit = debit + (average-temp->base_class->cur_exp);
                  temp->prev_debit = debit;
/*line no. 238*/                  fprintf(fp_data, "%d %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                  }
                }
              }
             }
            }

Thnx..
0
van_dyCommented:
it would be easier if you could explain the significance of
list_ptr in your functions
0
ksanand_beAuthor Commented:
Basically what I am trying to do is, to create linked list, accept the data and write it into a file. Then for calculation, again I create a linked list and read the required values from the corresponding file and write the updated values (debit and credit) into the data.txt

So list_ptr is the address where the list starts.

However now the I can read from the file however there is still an indefinite loop as mentioned before. I think I have to watch a few variables to correct the problem.

Thnx.
0
van_dyCommented:
Hmm,

>>Also I'm getting an indefinite loop in the following section of the code.

hmm, may be this will help


while(temp){  //check if valid pointer
if(temp->base_class->cur_exp == average)
              {
               temp->prev_debit = 0.0;
               temp->prev_credit = 0.0;
 /*line no. 224 */               fprintf(fp_data,"%s %2f %2f",temp->base_class->name, temp->prev_debit, temp->prev_credit);//error: Illegal use of floating point
               }
             else if(temp->base_class->cur_exp > average)
               {
                debit = 0;
                credit = credit + (temp->base_class->cur_exp - average);
                temp->prev_credit = credit;
/*line no. 231*/                fprintf(fp_data, "%s %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                 }
              else
               {
                credit = 0;
                debit = debit + (average-temp->base_class->cur_exp);
                temp->prev_debit = debit;
/*line no. 238*/                fprintf(fp_data, "%d %f %f",temp->base_class->name,temp->prev_debit, temp->prev_credit);
                }
      temp = temp->next;        //proceed to next element in the list
}
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.