Working with text files.How to extract a specific line from a text file.
17 Replies - 1608 Views - Last Post: 02 December 2010 - 09:41 PM
#1
Working with text files.
Posted 02 December 2010 - 12:01 AM
So if I had...
Abe
Bob
Carl
Derik
Gary
Jack
Laura
Mike
I know where everyone should appear in my binary tree. I'd plan to do this by assigning each person a number based on what line they appear at. So we have
1) Abe <---Head
2) Bob
3) Carl
4) Derik
5) Gary <---pivot
6) Jack
7) Laura
8) Mike <---Tail
So taking (1+9)/2 gives us 4.5 or 5. Therefore when loading data from this text file we first insert Gary into the binary tree because it is evident he will be the root.
Now work on the left tree.
(1+4)/2 = 2.5 or 3. So insert Carl next.
(1+2)/2 = 1.5 or 2. So insert Bob.
(1+1)/2 = 1 so insert Abe.
Backtracking a bit next insert 4 and the left tree is done.
While I am still working on the code to do this what I need to know is
1) Is it possible to pick a specific line from a text file(how would I do this), or
2) Would it just be easier to load things into an array first and work with the array?
Though with option 2 this would require more memory... so I'd rather use option 1.
Thanks in advance for any help.
Replies To: Working with text files.
#2
Re: Working with text files.
Posted 02 December 2010 - 12:13 AM
#3
Re: Working with text files.
Posted 02 December 2010 - 12:19 AM
alias120, on 01 December 2010 - 11:13 PM, said:
The only thing I really know is that the list I am given will be in alphabetical order. I don't know the size until I count the lines. This makes me think I should load everything into a temporary array which would also make the recursive part easier.
#4
Re: Working with text files.
Posted 02 December 2010 - 12:38 AM
This post has been edited by alias120: 02 December 2010 - 12:41 AM
#5
Re: Working with text files.
Posted 02 December 2010 - 12:48 AM
Here is what I have at this moment.
void book :: load_data()
{
vector<string> nameVector;
vector<string> numberVector;
string temporary_name;
string temporary_number;
ifstream inFile("data.txt");
if(inFile.fail())
{
cout << "File is corrupted!";
return;
}
else
{
int size = 0;
string line;
while (inFile.peek() != EOF)
{
inFile >> temporary_name >> temporary_number;
nameVector.push_back(temporary_name);
numberVector.push_back(temporary_number);
// add(temporary_name, temporary_number);
}
}
inFile.close();
}
I have read in the file to two vectors so now I am working on the algorithm to insert them into the binary tree correctly. Anyways, thanks for the help so far.
This post has been edited by Riskinit: 02 December 2010 - 12:52 AM
#6
Re: Working with text files.
Posted 02 December 2010 - 01:00 AM
#7
Re: Working with text files.
Posted 02 December 2010 - 02:44 AM
The following is giving errors.
Function declaration.
void book :: findNextValue(int myArray[], int left, int right, int index, vector<string> &nameVector, vector<string> &numberVector)
Function prototype.
void findNextValue(int myArray[], int left, int right, int index, vector<string> &a, vector<string> &B)/>;
Call to function.
findNextValue(myArray, left, right, index, &nameVector, &numberVector);
Specifically I am getting the following 2 errors on all 3 of my calls.
Error 1 error C2664: 'book::findNextValue' : cannot convert parameter 5 from 'std::vector<_Ty> *' to 'std::vector<_Ty> &'
4 IntelliSense: initial value of reference to non-const must be an lvalue
#8
Re: Working with text files.
Posted 02 December 2010 - 04:05 AM
#9
Re: Working with text files.
Posted 02 December 2010 - 04:59 AM
&nameVector, &numberVector);
Lose the &s. The references are part of the function prototype, you do not need to pass the address of the variables to the function.
#10
Re: Working with text files.
Posted 02 December 2010 - 07:47 AM
Basically I am trying to insert specific values of a linked list when I know certain values will be certain nodes in a binary tree.
Below is the full code.
EDITED OUT CODE...
My problem is the LAST two functions in phonebook.cpp. I can can get the left branch of my tree setup but when I start backtracking it won't add the correct values. In the case of the following data the first 3 values all go to the left tree but the right branch is never formed.
data.txt
Alex 55555
Brad 55555
Gilg 77777
Sara 11111
If anyone sees something I am doing wrong please point it out.
This post has been edited by Riskinit: 02 December 2010 - 11:31 AM
#11
Re: Working with text files.
Posted 02 December 2010 - 08:05 AM
case 6://save and exit phoneBook.save(); exit(1); Sleep(2000); cout << "Saving..." << endl; cout << "Done" << endl; break; case 7: int main(); break; }
For "case 6:" Why are you exiting the program telling the operating system that there was an error (exit(1)). Calling exit() in C++ programs is considered bad because it does not call class destructors. Also the remaining lines after the "exit(1)" will never be reached as you told the program to stop here.
For "case 7:" In C++ should never call main() from any function. In the case of this program a simple return here and a loop in main() would be better.
Jim
#12
Re: Working with text files.
Posted 02 December 2010 - 08:16 AM
int main();
is NOT how you call a function.
#13
Re: Working with text files.
Posted 02 December 2010 - 08:21 AM
Thank you for the input.
{.........
case 6://save and exit
phoneBook.save();
Sleep(2000);
cout << "Saving..." << endl;
cout << "Done" << endl;
break;
case 7:
return;
break;
}
return;
This post has been edited by Riskinit: 02 December 2010 - 08:22 AM
#14
Re: Working with text files.
Posted 02 December 2010 - 11:22 AM
When using
nameVector.at(index-1);
Does it access the vector elements?
So if I had a vector with the values
[1] [2] [3] [4]
and said...
nameVector.at(1);
this would display the value 2? Is this correct?
#15
Re: Working with text files.
Posted 02 December 2010 - 11:47 AM
|
|

New Topic/Question
Reply



MultiQuote





|