Working with text files.

How to extract a specific line from a text file.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2310 Views - Last Post: 02 December 2010 - 09:41 PM Rate Topic: -----

#1 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Working with text files.

Posted 02 December 2010 - 12:01 AM

Ok, basically I am trying to extract specific lines from a text file. This is so I can quickly generate a relatively balanced binary tree from an alphabetical list of names.

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Working with text files.

#2 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Working with text files.

Posted 02 December 2010 - 12:13 AM

When iterating through each line of the text file, if you know how the file is formatted before hand, you could compare that line read with an expected input. If that line contains what you need, handle accordingly. If not, discard and continue searching.
Was This Post Helpful? 0
  • +
  • -

#3 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 12:19 AM

View Postalias120, on 01 December 2010 - 11:13 PM, said:

When iterating through each line of the text file, if you know how the file is formatted before hand, you could compare that line read with an expected input. If that line contains what you need, handle accordingly. If not, discard and continue searching.


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.
Was This Post Helpful? 0
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Working with text files.

Posted 02 December 2010 - 12:38 AM

Unless you obtain the number of lines in the file before hand, an array might not be the best option. Perhaps a vector would work better in this case. In the case of a binary tree, reading the file into a vector would be alright for files of no substantial size. Anything too large might need a different method, though i'm honestly not sure exactly which.

This post has been edited by alias120: 02 December 2010 - 12:41 AM

Was This Post Helpful? 1
  • +
  • -

#5 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 12:48 AM

I was actually just refreshing my memory on vectors to do this. I'm not really sure either what the best ways to do things are but this is just a project for school so it isn't that big of a deal. I'd think the biggest size of list I'll deal with might be 20 lines???

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

Was This Post Helpful? 0
  • +
  • -

#6 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Working with text files.

Posted 02 December 2010 - 01:00 AM

If I think of something I'll get back to you, though i'm sure someone much more experienced than myself will respond to your question first.
Was This Post Helpful? 0
  • +
  • -

#7 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 02:44 AM

I've come to the point I have to get some sleep. Quick question if someone could help me though, how do I pass a vector by reference?

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
Was This Post Helpful? 0
  • +
  • -

#8 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Working with text files.

Posted 02 December 2010 - 04:05 AM

Your call to the function has the dereference operand as part of the arguement for your vectors.
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

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.
Was This Post Helpful? 1
  • +
  • -

#10 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 07:47 AM

If anyone can beat me to where my logic fails I will appreciate it very much. I finished what I thought was the correct algorithm this morning and am on the move between classes.

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

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,293
  • Joined: 25-December 09

Re: Working with text files.

Posted 02 December 2010 - 08:05 AM

In your code you have:

		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
Was This Post Helpful? 1
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Working with text files.

Posted 02 December 2010 - 08:16 AM

Not to mention this
int main();

is NOT how you call a function.
Was This Post Helpful? 1
  • +
  • -

#13 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 08:21 AM

Fixed:
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

Was This Post Helpful? 0
  • +
  • -

#14 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Working with text files.

Posted 02 December 2010 - 11:22 AM

I need to confirm something about vectors because it is messing with me.

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?
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1398
  • View blog
  • Posts: 4,873
  • Joined: 19-February 09

Re: Working with text files.

Posted 02 December 2010 - 11:47 AM

View PostRiskinit, on 02 December 2010 - 07:22 PM, said:

this would display the value 2? Is this correct?


Yes. :shuriken:
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2