| | |
linked lists & files...plz help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
i've created a linked list which stores some info about documents
such as code,title,creator,filename,...
i've also stored some info about some other documents in a *.txt file
i've got a search function in my program that gets a code from the user and
tries to find that code either in the file or in the linked list....when it searches the
linked list it works alright and gives all the other info of that document, however
when searching the file, the program runs an error and ends
also, i cannot write more than one series of information at a time on the file
can u help?
such as code,title,creator,filename,...
i've also stored some info about some other documents in a *.txt file
i've got a search function in my program that gets a code from the user and
tries to find that code either in the file or in the linked list....when it searches the
linked list it works alright and gives all the other info of that document, however
when searching the file, the program runs an error and ends
also, i cannot write more than one series of information at a time on the file
can u help?
C++ Syntax (Toggle Plain Text)
refs::refs() { next=NULL; first=last=NULL; f.open("refs.txt", ios::in|ios::out|ios::app|ios::binary); cout<<"\nEnter the properties of the document in the following order : "<<endl; cout<<"\n\t Code: "<<endl; cin>>code; int l=strlen(code); f<<"\n"; f.write((unsigned char*)code,l); f<<"\t"; cout<<"\n\t Title: "<<endl; cin>>title; int l1=strlen(title); f.write((unsigned char*)title,l1); f<<"\t"; }
C++ Syntax (Toggle Plain Text)
void refs::search() { char cd[6]; char ttl[40]; char cr[40]; char lc[100]; char fn[60]; char kw[40]; char cm[255]; char s; while(1) { cout<<"\n\nPlease determine how you want to search :"; cout<<"\nTo search by Code press 'c'\nTo search by Title press 't'\nTo search by Creator press 'r'"; cout <<"\nTo search by Location press 'l'\nTo search by file name press 'n'\n"; cout <<"To search by Keyword press 'k'\nTo search by Comment press 'm'\n"<<endl; cin>>s; char str[200]; refs *ptr; ptr=first; while(ptr){ switch(s) { case 'c': { cout<<"\nPlease enter the Code you want to search for: "<<endl; cin>>cd; int len=strlen(cd); for(int ii=0; ii<len ;ii++) { if(ptr->code[ii]==cd[ii]) { cout<<endl<<"*****Code found in the list .\n"; cout<<endl<<"**************************"; cout<<"Here is the full information of the document: "<<endl; cout<<"\n\tCode :"<<ptr->code<<"\n \tTitle : "<<ptr->title<<"\n\tCreator : "<<ptr->creator; cout<<"\n\tLocation :"<<ptr->location<<"\n \tFile Name : "<<ptr->filename<<"\n\tKeyword : "<<ptr->keyword; cout<<"\n\tComment "<<ptr->comment; cout<<endl<<"**************************"; break; } ptr=ptr->next; } while (f.getline(str,120)) { int lenn=strlen(str); for(int j=0;j<lenn;j++) if(cd[j]==str[j]) { cout<<endl<<"*****Code found in the list .\n"; cout<<endl<<"**************************"; cout<<"Here is the full information of the document: "<<endl; cout<<"\n\tCode :"<<ptr->code<<"\n \tTitle : "<<ptr->title<<"\n\tCreator : "<<ptr->creator; cout<<"\n\tLocation :"<<ptr->location<<"\n \tFile Name : "<<ptr->filename<<"\n\tKeyword : "<<ptr->keyword; cout<<"\n\tComment "<<ptr->comment; cout<<endl<<"**************************"; break; } } } break; . . . .
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
this assumes you have data followed by a whitespace
hope that helps...
cpp Syntax (Toggle Plain Text)
ifstream myFile; //assuming you have named your input stream myFile myFile.open("file.txt"); struct myStruct{ string data_one; string data_two; myStruct* link; } typedef myStruct *myPtr; myPtr head=NULL; myPtr tmp; string file_data_one, file_data_two,tmp_str; while(!myFile.eof()) { myFile >>file_data_one; getline(myFile, file_data_one); tmp_str=file_data_one.substr(file_data_one.find(' ')+1,20); //gets the next 20 integers after the whitespace. istringstream cin(tmp_str); cin>>file_data_two; // converts the string "tmp_num" to the integer type and inputs it in num tmp = new myStruct; tmp -> data_one = file_data_one; tmp -> data_two=file_data_two; tmp -> link=head; head=tmp; } myfile.close();
•
•
Join Date: Oct 2007
Posts: 280
Reputation:
Solved Threads: 19
Iam not well versed with classes at the moment so i cannot conver it for you..I believe they kind of work in the same way and if you have the idea then it should help you convert it
>>for some of your code my compiler gives error
well you were not supposed to copy paste the code..you were to get the idea from here and use it in your program..what errors are you getting and what kind of data are you working with in your file
>>for some of your code my compiler gives error
well you were not supposed to copy paste the code..you were to get the idea from here and use it in your program..what errors are you getting and what kind of data are you working with in your file
![]() |
Other Threads in the C++ Forum
- Previous Thread: declaring linked lists as extern
- Next Thread: Vector trouble
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





