954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

linked lists & files...plz help!

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?

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";

}
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;
.
.
.
.
scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

well you could read in the details from the file using "getline()" or you could store the details of the file as a linked list, then serach that list

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

i've tried getline() but it runs an error and the program ends
how can i store the contents of my file in a linked list?

scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
how can i store the contents of my file in a linked list?

this assumes you have data followed by a whitespace

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();

hope that helps...

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

could you plz write your code using a class
for some of your code my compiler gives error
(PS. i use a Microsoft Visual C++ 6)

scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
could you plz write your code using a class


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

joshmo
Posting Whiz in Training
280 posts since Oct 2007
Reputation Points: 19
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You