Solved

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

Posted on 2002-07-04
9
245 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
Independent Software Vendors: 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 126
C++ mouse_event mouse look 7 131
print bytes of an integer 6 48
C++ statement T∗ begin(Vector<T>& x) 5 42
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

737 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