?
Solved

Reading from a file

Posted on 2004-11-16
3
Medium Priority
?
277 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:ksanand_be
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 12596277

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
 

Author Comment

by:ksanand_be
ID: 12596396
Oops! That was a terrible terrible silly mistake. Thankyou very much.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 12596611

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question