Solved

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

Posted on 2002-07-04
9
241 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
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
Technology Partners: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

685 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