linked list and text file

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2008
Posts: 3
Reputation: Kidd87 is an unknown quantity at this point 
Solved Threads: 0
Kidd87 Kidd87 is offline Offline
Newbie Poster

linked list and text file

 
0
  #1
Sep 24th, 2008
When I excute my program and open up the text file, it only reads me the 1234 then junk below it. I have no clue what do. Anything is welcome. Below is my .cpp file

my text file looks something like this

1234 jack hammer 75.00 .45 10
5678 Daffy Drill 25.00 .67 10
9887 Flash light 30.00 .87 10



  1. #include<iostream>
  2. #include<conio.h>
  3. #include<fstream>
  4. #include<iomanip>
  5. using namespace std;
  6.  
  7. //******************************************************************************
  8.  
  9. struct acmetype{
  10. int pid;
  11. char description[25];
  12. float wholesale;
  13. float markup;
  14. int quantity;
  15. acmetype *link;
  16. };
  17.  
  18. ifstream fin; //global variable
  19.  
  20. void headerfn();
  21. void fileopen();
  22. void inputfn(acmetype *&head);
  23. void printfn(acmetype *&head);
  24.  
  25.  
  26. //*******************
  27. //* Begin of Main *
  28. //*******************
  29.  
  30. int main(){
  31. system("color F0");
  32. headerfn();
  33. fileopen();
  34.  
  35. acmetype *head;
  36. inputfn(head);
  37. printfn(head);
  38.  
  39.  
  40.  
  41. cout<<endl<<endl;
  42. system("PAUSE");
  43.  
  44. }//end of main
  45.  
  46. //******************************************************************************
  47. void fileopen(){
  48. string tools;
  49. int pid;
  50. char description;
  51. fin.open("tools.txt");
  52.  
  53. if (!fin)
  54. {
  55. cout << "Error opening output file. Go Play Basketball like Kobe!!"<<endl<<endl;
  56. system("PAUSE");
  57. getch();
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64. //***************************************************************************
  65. void inputfn(acmetype *&head){
  66. acmetype *currptr, *nnptr;
  67. int counter= 0;
  68. char description[25];
  69.  
  70. head = new acmetype;//creating first node
  71. fin>>head->pid;
  72. currptr = head;//curr ptr pointing at head and first node is ready.
  73. counter++;
  74. while(fin){
  75. nnptr= new acmetype;
  76. fin>>nnptr->pid;
  77. fin.getline(description, 25);
  78. // fin.getline(acmetype.description[25]);
  79.  
  80. currptr->link = nnptr;
  81. currptr = currptr->link;
  82. if(fin.peek()=='\n')fin.ignore();//check for next character
  83. counter++;
  84. }//end of while
  85. currptr->link= NULL;
  86. currptr= NULL;
  87. nnptr= NULL;
  88.  
  89. }//end of inputfn
  90. //****************************************************************************
  91. void printfn(acmetype *&head){
  92. acmetype *currptr;
  93.  
  94. //print all data members
  95. currptr = head;
  96. while(currptr!=NULL){
  97. cout<<currptr->pid<<endl;
  98. currptr=currptr->link;
  99. if(fin.peek()=='\n')fin.ignore();
  100. }//end of while
Last edited by Ancient Dragon; Sep 24th, 2008 at 9:48 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list and text file

 
0
  #2
Sep 24th, 2008
line 77: >> fin.getline(description, 25);

Unfortunately that doesn't work because the description field is not a fixed size of 25 characters in the data file. What you need to do is just read the next two words and concantinate them together: something like this
  1. std::string temp;
  2. fin >> nnptr->description;
  3. fin >> temp; temp = " " + temp;
  4. strcat(nnptr->description, temp.c_str();
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: Kidd87 is an unknown quantity at this point 
Solved Threads: 0
Kidd87 Kidd87 is offline Offline
Newbie Poster

Re: linked list and text file

 
0
  #3
Sep 24th, 2008
1234 jack hammer 75.00 .45 10
5678 Daffy Drill 25.00 .67 10
9887 Flash light 30.00 .87 10
4567 Carbide tip blade 13.95 .50 10
5678 sli welder 375.00 .25 5

would that make it description[25]? I really appreciate the feed back.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,661
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1501
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: linked list and text file

 
0
  #4
Sep 24th, 2008
getline() reads 25 charcters, or up to end-of-line, which ever is shorter. In order for that to work the description field in the data file must always be 25 characters (fixed length). The newest file example you just posted has 3 words in some description fields, which tosses out my previous suggestion. Here is how I would do that NOTE: This has not been compiled or tested.
  1. std::string line;
  2. while( getline(fin, line) )
  3. {
  4. size_t pos = 0;
  5. // find first non-digit character
  6. while( idsigit(line[pos]) )
  7. pos++;
  8. // extract pid
  9. nnptr->pid = atol( line.substr(0,pos).c_str());
  10. line = line.substr(pos+1);
  11. // find first digit field (end of description)
  12. for(pos = 0; !isdigit(line[pos]); pos++)
  13. ;
  14. // extract description
  15. strcpy(nnptr->description, line.substr[0,pos].c_str());
  16. line = line.substr(pos);
  17. // convert remainint integers
  18. strstring str(line);
  19. str >> nnptr->wholesale >> nnptr->markup >> nnptr->quantity;
  20. // add nnptr to linked list
  21. <snip>
  22. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 960 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC