C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Struct infile Posted by EPenguin on 22 Apr 2006 at 10:07 PM
I'm infiling a struct and for whatever reason I just can't figure out what I need to do to get this to work.

int main(){

 char cdNum[11];
 char cdTitle[41];
 long qtyOnHand;
 double cost;
 CDRec* Inventory;
 int count=0;
 
 ifstream infile("cd.txt");
 
 if(!infile){
  cerr << "Error: File does not exist\n";
  exit(0);
 }
char c;
 while(infile.get(cdNum, sizeof(cdNum))){
  if(infile.eof())
	  break;
  
  infile>>ws;
  infile.get(cdTitle, sizeof(cdTitle));
  infile >> qtyOnHand >> cost;
  infile>>ws;
  Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);
  count++;
 } 
infile.close();
for(int i =0; i < count; i++){
	cout << Inventory[i].cdNum << setw(13) << Inventory[i].cdTitle << setw(43) << Inventory[i].qtyOnHand << setw(5) << Inventory[i].cost <<endl;
}
 return 0;
}


The problem seems to be the while loop. I'm not used to inputting a char string first. The input of the struct works fine. Any help would be greatly appreciated.
Report
Re: Struct infile Posted by Donotalo on 23 Apr 2006 at 10:26 PM
: I'm infiling a struct and for whatever reason I just can't figure out what I need to do to get this to work.
:
:
: int main(){
: 
:  char cdNum[11];
:  char cdTitle[41];
:  long qtyOnHand;
:  double cost;
:  CDRec* Inventory;
:  int count=0;
:  
:  ifstream infile("cd.txt");
:  
:  if(!infile){
:   cerr << "Error: File does not exist\n";
:   exit(0);
:  }
: char c;
:  while(infile.get(cdNum, sizeof(cdNum))){
:   if(infile.eof())
: 	  break;
:   
:   infile>>ws;
:   infile.get(cdTitle, sizeof(cdTitle));
:   infile >> qtyOnHand >> cost;
:   infile>>ws;
:   Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);
:   count++;
:  } 
: infile.close();
: for(int i =0; i < count; i++){
: 	cout << Inventory[i].cdNum << setw(13) << Inventory[i].cdTitle << setw(43) << Inventory[i].qtyOnHand << setw(5) << Inventory[i].cost <<endl;
: }
:  return 0;
: }
: 

:
: The problem seems to be the while loop. I'm not used to inputting a char string first. The input of the struct works fine. Any help would be greatly appreciated.
:

ur style of keeping CDRec items in Inventory is errorneous. u must allocate sufficient memory for Inventory to hold all the items found in the file.
what is ws? it is not defined. why r u using it?
make ur code organized while reading from a file. read all Inventory values, then check for the end of file.
u shud also mention clearly what is the expected output u r looking for.


~Donotalo()

Report
Re: Struct infile Posted by EPenguin on 26 Apr 2006 at 10:27 AM
: : I'm infiling a struct and for whatever reason I just can't figure out what I need to do to get this to work.
: :
: :
: : int main(){
: : 
: :  char cdNum[11];
: :  char cdTitle[41];
: :  long qtyOnHand;
: :  double cost;
: :  CDRec* Inventory;
: :  int count=0;
: :  
: :  ifstream infile("cd.txt");
: :  
: :  if(!infile){
: :   cerr << "Error: File does not exist\n";
: :   exit(0);
: :  }
: : char c;
: :  while(infile.get(cdNum, sizeof(cdNum))){
: :   if(infile.eof())
: : 	  break;
: :   
: :   infile>>ws;
: :   infile.get(cdTitle, sizeof(cdTitle));
: :   infile >> qtyOnHand >> cost;
: :   infile>>ws;
: :   Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);
: :   count++;
: :  } 
: : infile.close();
: : for(int i =0; i < count; i++){
: : 	cout << Inventory[i].cdNum << setw(13) << Inventory[i].cdTitle << setw(43) << Inventory[i].qtyOnHand << setw(5) << Inventory[i].cost <<endl;
: : }
: :  return 0;
: : }
: : 

: :
: : The problem seems to be the while loop. I'm not used to inputting a char string first. The input of the struct works fine. Any help would be greatly appreciated.
: :
:
: ur style of keeping CDRec items in Inventory is errorneous. u must allocate sufficient memory for Inventory to hold all the items found in the file.
: what is ws? it is not defined. why r u using it?
: make ur code organized while reading from a file. read all Inventory values, then check for the end of file.
: u shud also mention clearly what is the expected output u r looking for.
:

:
~Donotalo()
:
:
Thanks for the help everyone, I ended up just rewriting the input so that it was a function and tested for the eof that way.


ws is defined in Visual Studio as "white space".
Report
Re: Struct infile Posted by tsagld on 27 Apr 2006 at 12:26 AM
: : : I'm infiling a struct and for whatever reason I just can't figure out what I need to do to get this to work.
: : :
: : :
: : : int main(){
: : : 
: : :  char cdNum[11];
: : :  char cdTitle[41];
: : :  long qtyOnHand;
: : :  double cost;
: : :  CDRec* Inventory;
: : :  int count=0;
: : :  
: : :  ifstream infile("cd.txt");
: : :  
: : :  if(!infile){
: : :   cerr << "Error: File does not exist\n";
: : :   exit(0);
: : :  }
: : : char c;
: : :  while(infile.get(cdNum, sizeof(cdNum))){
: : :   if(infile.eof())
: : : 	  break;
: : :   
: : :   infile>>ws;
: : :   infile.get(cdTitle, sizeof(cdTitle));
: : :   infile >> qtyOnHand >> cost;
: : :   infile>>ws;
: : :   Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);
: : :   count++;
: : :  } 
: : : infile.close();
: : : for(int i =0; i < count; i++){
: : : 	cout << Inventory[i].cdNum << setw(13) << Inventory[i].cdTitle << setw(43) << Inventory[i].qtyOnHand << setw(5) << Inventory[i].cost <<endl;
: : : }
: : :  return 0;
: : : }
: : : 

: : :
: : : The problem seems to be the while loop. I'm not used to inputting a char string first. The input of the struct works fine. Any help would be greatly appreciated.
: : :
: :
: : ur style of keeping CDRec items in Inventory is errorneous. u must allocate sufficient memory for Inventory to hold all the items found in the file.
: : what is ws? it is not defined. why r u using it?
: : make ur code organized while reading from a file. read all Inventory values, then check for the end of file.
: : u shud also mention clearly what is the expected output u r looking for.
: :

: :
~Donotalo()
: :
: :
: Thanks for the help everyone, I ended up just rewriting the input so that it was a function and tested for the eof that way.
:
:
: ws is defined in Visual Studio as "white space".
:

Not sure what you did, but you still have a major memory leak issue in your code.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl/english/E_index.htm


Report
Re: Struct infile Posted by tsagld on 24 Apr 2006 at 1:50 AM
: I'm infiling a struct and for whatever reason I just can't figure out what I need to do to get this to work.
:
:
: int main(){
: 
:  char cdNum[11];
:  char cdTitle[41];
:  long qtyOnHand;
:  double cost;
  CDRec** Inventory = calloc(1024, sizeof(CDRec*)) //alloc space for 1024 pointers.
:  int count=0;
:  
:  ifstream infile("cd.txt");
:  
:  if(!infile){
:   cerr << "Error: File does not exist\n";
:   exit(0);
:  }
: char c;
:  while(infile.get(cdNum, sizeof(cdNum))){
:   if(infile.eof())
: 	  break;
:   
:   infile>>ws;
:   infile.get(cdTitle, sizeof(cdTitle));
:   infile >> qtyOnHand >> cost;
:   infile>>ws;
   Inventory[count] = new CDRec(cdNum, cdTitle, qtyOnHand, cost); 
:   count++;
:  } 
: infile.close();
: for(int i =0; i < count; i++){
: 	cout << Inventory[i].cdNum << setw(13) << Inventory[i].cdTitle << setw(43) << Inventory[i].qtyOnHand << setw(5) << Inventory[i].cost <<endl;

/* Don't forget to release memory!!!!
  for(i =0; i < count; i++){
    delete Inventory[i];
  }
  free(Inventory);

: }
:  return 0;
: }
: 

:
: The problem seems to be the while loop. I'm not used to inputting a char string first. The input of the struct works fine. Any help would be greatly appreciated.
:
With your old line
Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);

you lose the previous CDRec, and create a memory leak.
The problem is that you want Inventory to be an array of CDRec's but you are using it as a pointer to one instance of CDRec.
I corrected that in the red lines in the above code.


Greets,
Eric Goldstein
www.gvh-maatwerk.nl





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.