• C

Reading from a file

Hi,

I have pasted my codes below. The first set of codes is a file called starter.cpp where I initialise the values of a structure in a file called data.txt. The second set of code is a function called display where I retreive the values from the file data.txt and is in a different module.

The problem is that I am able to write the values correctly. When reading the valuesin the display function, only the names are read but not the initialised values (prev_credit and prev_debit). Kindly help me.

Starter.cpp
------------

extern "C" {
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
}
struct acc_data
 {
   char *name;
   float prev_debit;
   float prev_credit;
   struct acc_data *next;
  };
int setnum();
void main()
 {
  FILE *fp_data, *fp_stat;
  int i,num;
  fp_data = fopen("c:\\tcplus\\tc\\data.txt","w");
  fp_stat = fopen("c:\\tcplus\\tc\\statistics.txt","w");
  struct acc_data *acc_ptr, *temp, *new_blk;
  acc_ptr = NULL;
  temp = acc_ptr;
  num = setnum();
  fprintf(fp_stat,"Number of people in the house = %d", num);
  for(i=1; i<=num; i++)
   {
    new_blk = (struct acc_data *)malloc(sizeof(struct acc_data));
    printf("Enter the name of person %d -",i);
    scanf("%s",new_blk->name);
    new_blk->prev_debit = new_blk->prev_credit = 0.0;
    fprintf(fp_data,"%s %f %f ",new_blk->name, new_blk->prev_debit, new_blk->prev_credit);
    fprintf(fp_data,"\n");
    new_blk->next = NULL;
    if(acc_ptr == NULL)
      {
     temp = new_blk;
     acc_ptr = temp;
       }
     else
       {
      temp->next = new_blk;
      temp = temp->next;
       }




}
fclose(fp_data);
fclose(fp_stat);
}
int setnum()
  {
    int number;
    clrscr();
    printf("Enter the number of people in your house ");
    scanf("%d",&number);
    return(number);
    }

display function in account.cpp
-----------------------------------
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));
   new_blk->base_class = (acc_cur *)malloc(sizeof(acc_cur));
   fscanf(fp_data,"%s %f %f \n", new_blk->base_class->name, new_blk->prev_debit, new_blk->prev_credit);

    fprintf(stdout,"%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;
     }*/

  }

Structure definitions for your reference
----------------------------------------------
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;




KINDLY HELP.

THNKS.
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.

Kent OlsenData Warehouse Architect / DBACommented:

fscanf(), scanf(), etc (the entire family of functions) require an address list to know where to store the conversion.  For strings, arrays, etc the name is, by definition, an address.  For int, long, short, float, etc the name represents a value.  You need to pass the address of these types

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

should be:

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



Good Luck!
Kent
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
ksanand_beAuthor Commented:
Oops! That was a terrible terrible silly mistake. Thankyou very much.
0
Kent OlsenData Warehouse Architect / DBACommented:

It catches a lot of us.  Though finding it is a lot easier when scanf() stores into reserved memory and causes a segmentation fault!


Kent
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.