Struct infile - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Struct infile

EPenguinEPenguin Posts: 23Member
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.

[code]
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
";
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;
}
[/code]

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.

Comments

  • DonotaloDonotalo Posts: 715Member
    : 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.
    :
    : [code]
    : 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
    ";
    : 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;
    : }
    : [/code]
    :
    : 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.
    :
    [purple]
    ur style of keeping [blue]CDRec[/blue] items in [blue]Inventory[/blue] is errorneous. u must allocate sufficient memory for [blue]Inventory[/blue] to hold all the items found in the file.
    what is [blue]ws[/blue]? it is not defined. why r u using it?
    make ur code organized while reading from a file. read all [blue]Inventory[/blue] values, then check for the end of file.
    u shud also mention clearly what is the expected output u r looking for.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • tsagldtsagld Posts: 621Member
    : 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.
    :
    : [code]
    : int main(){
    :
    : char cdNum[11];
    : char cdTitle[41];
    : long qtyOnHand;
    : double cost;
    [red] CDRec** Inventory = calloc(1024, sizeof(CDRec*)) //alloc space for 1024 pointers.[/red]
    : int count=0;
    :
    : ifstream infile("cd.txt");
    :
    : if(!infile){
    : cerr << "Error: File does not exist
    ";
    : 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;
    [red] Inventory[count] = new CDRec(cdNum, cdTitle, qtyOnHand, cost); [/red]
    : 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;
    [red]
    /* Don't forget to release memory!!!!
    for(i =0; i < count; i++){
    delete Inventory[i];
    }
    free(Inventory);
    [/red]
    : }
    : return 0;
    : }
    : [/code]
    :
    : 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
    [code]
    Inventory = new CDRec(cdNum, cdTitle, qtyOnHand, cost);
    [/code]
    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


  • EPenguinEPenguin Posts: 23Member
    : : 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.
    : :
    : : [code]
    : : 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
    ";
    : : 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;
    : : }
    : : [/code]
    : :
    : : 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.
    : :
    : [purple]
    : ur style of keeping [blue]CDRec[/blue] items in [blue]Inventory[/blue] is errorneous. u must allocate sufficient memory for [blue]Inventory[/blue] to hold all the items found in the file.
    : what is [blue]ws[/blue]? it is not defined. why r u using it?
    : make ur code organized while reading from a file. read all [blue]Inventory[/blue] values, then check for the end of file.
    : u shud also mention clearly what is the expected output u r looking for.
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :
    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".
  • tsagldtsagld Posts: 621Member
    : : : 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.
    : : :
    : : : [code]
    : : : 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
    ";
    : : : 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;
    : : : }
    : : : [/code]
    : : :
    : : : 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.
    : : :
    : : [purple]
    : : ur style of keeping [blue]CDRec[/blue] items in [blue]Inventory[/blue] is errorneous. u must allocate sufficient memory for [blue]Inventory[/blue] to hold all the items found in the file.
    : : what is [blue]ws[/blue]? it is not defined. why r u using it?
    : : make ur code organized while reading from a file. read all [blue]Inventory[/blue] values, then check for the end of file.
    : : u shud also mention clearly what is the expected output u r looking for.
    : : [/purple]
    : : [hr][purple]~Donotalo()[/purple]
    : :
    : :
    : 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


Sign In or Register to comment.