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
Solved

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

Posted on 2002-07-04
9
237 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 clear a vector as well as how to detect empty vectors in C++.

808 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