Solved

Problem reading text file and assign to array of object. Please help.

Posted on 2002-07-04
9
250 Views
Last Modified: 2010-04-01
My program always read more than three records and in my array, all records are same with last record.  Please help me to find out the problem.  Thanks first...

(check the loadData() method)

text file data; STUDENT, user1 and pass1 consider as on record, store in one account object.

STUDENT
user1
pass1
LECTURER
user2
pass2

the accountList contain more than 2 records.  when i displayed it, all is last records.
LECTURER
user2
pass2
LECTURER
user2
pass2
LECTURER
user2
pass2
.
.

I tried to assign directly to array..
accountList[getNoOfAcc()].setUserType(tType);
still the same.

class Account{
private:
  char *userType;
  char *userID;
  char *userPassword;
public:
  //getters and setters
}

class Bookshop{
private:
  Book bookList[arraySize];
  BookOrdered orderList[arraySize];
  BookRecommended recommendList[arraySize];
  Account accountList[arraySize];

  char *typeOfUser;
  char *id;
  int noOfAcc;
  int noOfBook;
  int noOfOrderBook;
  int noOfRecommendBook;
public:
  //setters
  void setID(char* tID){
    id=tID;
  };
  void setTypeOfUser(char* tType){
    typeOfUser=tType;
  };

  void setNoOfAcc(int tNoOfAcc){
    noOfAcc=tNoOfAcc;
  };

  Account getAccount(int index){
    return accountList[index];
  }
  void setAccount(Account aAccount, int index){
    accountList[index];
  }
  int getNoOfAcc(void){
    return noOfAcc;
  };  
  void setNoOfAcc(int tNoOfAcc){
    noOfAcc=tNoOfAcc;
  };
  void addAccount(Account aAccount, int index){
    accountList[index]=aAccount;
  };
}



void Bookshop::loadData(void){
  FILE *readFile;
  char* tType=new char[100];
  char* tID=new char[100];
  char* tPassword=new char[100];

  Account aAccount;
  setNoOfAcc(0);
   //read account
   readFile=fopen("c:\\account.txt", "r+");
   if(readFile==NULL){
     cout<<"Error reading account file..."<<endl;
     getch();
     exit(1);
   }
   while(!feof(readFile)){
     Account aAccount;
     fscanf(readFile,"%s",tType);
     aAccount.setUserType(tType);

     fscanf(readFile,"%s",tID);
     aAccount.setUserID(tID);

     fscanf(readFile,"%s",tPassword);
     aAccount.setPassword(tPassword);
     cout<<"NoOFAcc="<<getNoOfAcc()<<endl;getch();

     addAccount(aAccount,getNoOfAcc());
     setNoOfAcc(getNoOfAcc()+1);
   };
   fclose(readFile);

  //test
   cout<<"No of account "<<getNoOfAcc()<<endl;
     clrscr();
     for(int loop=0;loop<getNoOfAcc();loop++){
       aAccount=getAccount(loop);
       cout<<"TYPE="<<aAccount.getUserType()<<endl;
       cout<<"ID="<<aAccount.getUserID()<<endl;
       cout<<"Password="<<aAccount.getPassword()<<endl;
     };
     getch();
};

0
Comment
Question by:yongyih
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 7129890
The problem is that you are using pointers and you are overwriting them - use

#include <string.h>

class Bookshop{
private:
 Book bookList[arraySize];
 BookOrdered orderList[arraySize];
 BookRecommended recommendList[arraySize];
 Account accountList[arraySize];

 char *typeOfUser;
 char *id;
 int noOfAcc;
 int noOfBook;
 int noOfOrderBook;
 int noOfRecommendBook;
public:
 //setters
 void setID(char* tID){
   id= strdup ( tID);
 };
 void setTypeOfUser(char* tType){
   typeOfUser= strdup (tType);
 };

// ...

};

Of course, you'll have to 'free()' the memory when you are done.

instead
0
 
LVL 2

Author Comment

by:yongyih
ID: 7130815
To jkr,

  thank you thank you... when display, can display first record already.  But why program read extra record, which is same as the last one in text file?

STUDENT
user1
pass1
LECTURER
user2
pass2
LECTURER
user2
pass2

is it my checking for EOF has problem??

i have another 3 class in the program.  Does it mean every setter has to use strdup??

thanks a lot!!!!

0
 
LVL 86

Expert Comment

by:jkr
ID: 7130826
>>Does it mean every setter has to use strdup??

Actually, if you wanna go with char*, yes, you will. What about using STL strings?
0
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!

 
LVL 2

Author Comment

by:yongyih
ID: 7130833
Its ok for using strdup.  i just changed all the setters to strdup(..)

Now only remain two problems.
1. how to free the memory?
char* name=new char[256];
like this?
name=NULL;

2. Why need extra record?  
Is it because my text file has extra line

STUDENT
user1
pass1
LECTURER
user2
pass2
->cursor stop there.

this is the reason?  
0
 
LVL 2

Author Comment

by:yongyih
ID: 7130834
but when i delete the extra line, save it.  when i edit it again, the extra line come out automatically...

please help me. thanks. ^_^
0
 
LVL 2

Author Comment

by:yongyih
ID: 7130835
Thank you very much ! =)
0
 
LVL 86

Expert Comment

by:jkr
ID: 7130841
>>1. how to free the memory?
>>char* name=new char[256];
>>like this?
>>name=NULL;

No - everything that was allocated by "new[]" has to be deallocated using "delete[]" in your case

delete [] name;

NOTE that the strings duplicated by "strdup()" have to be released using "free()"

>>2. Why need extra record?  
>>Is it because my text file has extra line

This is probably the reason, as you check "feof()" - which makes your program assume that there still is a full record to read, even though it is just a Carriage Return.




0
 
LVL 2

Author Comment

by:yongyih
ID: 7130909
my program works correctly now. just that cannot read book name that has a space ("Book Name") will read as Book and Name..but never mind. ^_^

free().. where should i used it?
because i used strdup() and assign directly to object..
never mind.. as long as no errors =)

thanks..
0
 
LVL 86

Expert Comment

by:jkr
ID: 7131923
>> free().. where should i used it?

The best way for this would be in the destructors of your classes, e.g.

class Bookshop{

public:

 Bookshop () { typeOfUser = id = NULL;}
 ~Bookshop () { free ( typeOfUser); free ( id);}

// ...
};
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

729 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