Solved

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

Posted on 2002-07-04
9
212 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
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
 
LVL 2

Author Comment

by:yongyih
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:yongyih
Comment Utility
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
Comment Utility
Thank you very much ! =)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now