| | |
linked list and text file
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<conio.h> #include<fstream> #include<iomanip> using namespace std; //****************************************************************************** struct acmetype{ int pid; char description[25]; float wholesale; float markup; int quantity; acmetype *link; }; ifstream fin; //global variable void headerfn(); void fileopen(); void inputfn(acmetype *&head); void printfn(acmetype *&head); //******************* //* Begin of Main * //******************* int main(){ system("color F0"); headerfn(); fileopen(); acmetype *head; inputfn(head); printfn(head); cout<<endl<<endl; system("PAUSE"); }//end of main //****************************************************************************** void fileopen(){ string tools; int pid; char description; fin.open("tools.txt"); if (!fin) { cout << "Error opening output file. Go Play Basketball like Kobe!!"<<endl<<endl; system("PAUSE"); getch(); } } //*************************************************************************** void inputfn(acmetype *&head){ acmetype *currptr, *nnptr; int counter= 0; char description[25]; head = new acmetype;//creating first node fin>>head->pid; currptr = head;//curr ptr pointing at head and first node is ready. counter++; while(fin){ nnptr= new acmetype; fin>>nnptr->pid; fin.getline(description, 25); // fin.getline(acmetype.description[25]); currptr->link = nnptr; currptr = currptr->link; if(fin.peek()=='\n')fin.ignore();//check for next character counter++; }//end of while currptr->link= NULL; currptr= NULL; nnptr= NULL; }//end of inputfn //**************************************************************************** void printfn(acmetype *&head){ acmetype *currptr; //print all data members currptr = head; while(currptr!=NULL){ cout<<currptr->pid<<endl; currptr=currptr->link; if(fin.peek()=='\n')fin.ignore(); }//end of while
Last edited by Ancient Dragon; Sep 24th, 2008 at 9:48 pm. Reason: add code tags
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
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
C++ Syntax (Toggle Plain Text)
std::string temp; fin >> nnptr->description; fin >> temp; temp = " " + temp; 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.
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.
C++ Syntax (Toggle Plain Text)
std::string line; while( getline(fin, line) ) { size_t pos = 0; // find first non-digit character while( idsigit(line[pos]) ) pos++; // extract pid nnptr->pid = atol( line.substr(0,pos).c_str()); line = line.substr(pos+1); // find first digit field (end of description) for(pos = 0; !isdigit(line[pos]); pos++) ; // extract description strcpy(nnptr->description, line.substr[0,pos].c_str()); line = line.substr(pos); // convert remainint integers strstring str(line); str >> nnptr->wholesale >> nnptr->markup >> nnptr->quantity; // add nnptr to linked list <snip> }
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.
![]() |
Similar Threads
- Help with linked list in Turbo Pascal! (Pascal and Delphi)
- Linked List Problem (C++)
- Linked list help (C++)
- how to read a text file backwards? (C)
- Text file parser in C (C)
- ordered linked list (C)
- *strcpy and linked list (C#)
- struct linked list problem (C)
- How to read in a sentence and insert in to linked list? (C++)
Other Threads in the C++ Forum
- Previous Thread: Running dos command through c++
- Next Thread: Huffman...one last time :(
Views: 960 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






