By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Help with Vectors

P: n/a
Hi everyone

I am new to C++ so any help would be fantastic.

I need to know how you read a text file (with string data in) into a
vector or a list.

when it is read in I intend on letting the user search for a string and
then deleting it from the vector

Then the vector will be writen back to the original text file minus the
deleted text.

thank you for any help you provide.

BOB8000

Nov 7 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
bob8000 wrote:
I am new to C++ so any help would be fantastic.

I need to know how you read a text file (with string data in) into a
vector or a list.

when it is read in I intend on letting the user search for a string and
then deleting it from the vector

Then the vector will be writen back to the original text file minus the
deleted text.

thank you for any help you provide.
Use fstream, vector<string>, and getline(). If you need more specific
help, consult the FAQs (http://www.parashift.com/c++-faq-lite) and feel
free to ask more specific questions.

Cheers! --M

Nov 7 '06 #2

P: n/a
"bob8000" <bo*****@hotmail.co.ukwrote:
I need to know how you read a text file (with string data in) into a
vector or a list.
I wouldn't use a vector or list for this, I'd use a deque. Works like a
vector but tends to handle large chunks of memory better.
when it is read in I intend on letting the user search for a string and
then deleting it from the vector
How big of a string? A word, line, or an arbitrary bit of text? (I'm
going to assume the latter for now.)
Then the vector will be writen back to the original text file minus the
deleted text.

thank you for any help you provide.
The code below compiles and runs, but won't print "Working so far" until
you add code in the "load" function to make it work.

Check out:
http://www.dinkumware.com/manuals/?m...age=deque.html and
http://www.dinkumware.com/manuals/?m...e=istream.html for
information on how to use istream and deque.

Try out some different ideas and see what happens. Once you get "working
so far" to print, post what you did and I'll give you the next step.

If you have any further questions, post what you have so far and ask
away.

#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <iterator>
#include <sstream>
// add includes as needed

using namespace std;

void load( istream& is, deque<char>& data )
{
// insert code here
}

int main()
{
istringstream iss( "Hello world!" );
deque<chardata;
load( iss, data );
copy( data.begin(), data.end(), ostream_iterator<char>( cout, "" ) );
cout << "\nsize = " << data.size() << '\n';
assert( data.size() == 12 );
assert( data[0] == 'H' );
assert( data[6] == 'w' );
cout << "working so far\n";
}

--
To send me email, put "sheltie" in the subject.
Nov 7 '06 #3

P: n/a
"bob8000" <bo*****@hotmail.co.ukwrote in message
news:11*********************@m7g2000cwm.googlegrou ps.com...
Hi everyone

I am new to C++ so any help would be fantastic.

I need to know how you read a text file (with string data in) into a
vector or a list.

when it is read in I intend on letting the user search for a string and
then deleting it from the vector

Then the vector will be writen back to the original text file minus the
deleted text.

thank you for any help you provide.

BOB8000
Simplest case: Untested code

void main()
{
std::vector<std::stringFileVector;
std::ifstream MyFile( "MyFile.txt" );
if (ifstream.is_open() )
{
std::string Line;
while ( std::getline( Myfile, Line ) )
FileVector.push_back( Line );
}
}

Most likely that won't compile as is, probably a few syntax errors (is it
std::istream?) but you should get the idea.
Nov 8 '06 #4

P: n/a
Thanks everybody, you have all been most helpful.

I will have a play with you ideas and post my results soon.
At least I'm one step closer to home
BOB8000

Nov 8 '06 #5

P: n/a
I have work the code out now from the help you have given
ifstream Myfile; //declare new stream
vector<stringFileVector; //declare new vector

Myfile.open("custDetails.dat");
if (Myfile.is_open())
{
string Line;
while ( getline( Myfile, Line ) )
{
FileVector.push_back( Line );
cout << Line; //here just for me to check the vector was working
}
}
else
{
cout << "Error opening file";
}

It is now reading the file into the vector, Fantastic!!!!

Now how do I search for a user determined string within that vector.

Nov 8 '06 #6

P: n/a
"bob8000" <bo*****@hotmail.co.ukwrote:
I have work the code out now from the help you have given
ifstream Myfile; //declare new stream
vector<stringFileVector; //declare new vector

Myfile.open("custDetails.dat");
if (Myfile.is_open())
{
string Line;
while ( getline( Myfile, Line ) )
{
FileVector.push_back( Line );
cout << Line; //here just for me to check the vector was working
}
}
else
{
cout << "Error opening file";
}

It is now reading the file into the vector, Fantastic!!!!

Now how do I search for a user determined string within that vector.
Look up "search" in
http://www.dinkumware.com/manuals/?m...=algorith.html
and "find" in
http://www.dinkumware.com/manuals/?m...e=string2.html.

--
To send me email, put "sheltie" in the subject.
Nov 8 '06 #7

P: n/a
I have looked at the find() command and this seems to be more to do
with finding a range on
INT numbers and not a word in a string of words.

just this one last problem to solve and I will be sorted.

Any help please.

Nov 9 '06 #8

P: n/a

bob8000 wrote in message ...
>I have looked at the find() command and this seems to be more to do
with finding a range on INT numbers and not a word in a string of words.
just this one last problem to solve and I will be sorted.
Any help please.
So, 'find()' can only be used on 'INT numbers', eh? I didn't know that, so I
did:

// -------
#include <string>
// -------
{
std::string ReadFromFile(
"If I had a hammer, I'd hit myself and be done with Bush." );
std::string FindThis( "Bush" );

if( ReadFromFile.find( FindThis ) != std::string::npos ){
std::cout << "Found: " << FindThis << std::endl;
}
else{
std::cout << "Did not find " << FindThis
<<" in string:\n"<<ReadFromFile<< std::endl;
}
}
// -------

There's more than one kind of 'find()' in the C++ world!

Do you read posts other than ones in a thread you started?
You should!!!

--
Bob R
POVrookie
Nov 9 '06 #9

P: n/a
bob8000 wrote:
>
I have looked at the find() command and this seems to be more to do
with finding a range on
INT numbers and not a word in a string of words.

just this one last problem to solve and I will be sorted.
void foo( vector<string>& vec )
{
vector<string>::iterator it = find( vec.begin(), vec.end(), "hello"
);
assert( it == vec.end() || *it == "hello" );
}

Nov 10 '06 #10

P: n/a
Hello everyone

Thank you for the help that you have all provided on my vector
problems.

I have almost finish what I need my programme to do. It is now asking
the user for input and then finding what the user requests from the
vector. Excellent.

All I need help on now is how to remove the line of text from the
vector and I will be done.

This is what it looks like:

----------------------------------------------------------------------------------------
01 GUY HAWKINS BOBBY JOHN SAILING
02 ANNE HAWKINS HAWKEYE SAILING
03 JOHN LITTLE LITTLE JOHN SAILING
04 TINA ARENA FLOATING BOAT SAILING
05 ROSIE JAMES POLLY POCKET NARROW
06 JOHUA JONES QUACK MOTOR
07 SAMMY SQUIRAL HORACE NARROW
08 RONNIE BAKER WESTLAND SAILING
09 ANNE WINTERS FREDDIE SAILING
10 TONY CRAMP WELLYBOOT NARROW
11 WILLIAM BAXTER RABBIT NARROW
12 JASON BELL CONNIE NARROW

Enter record number to delete: 03

--------------------------------------------------------------------------------------
the user would then be promted if they wanted to remove record 03. then
if they said yes
the program wolud delete line 03 from the vector.

This is my 'find' code that you have all helped me with, Thank you.

Myfile.open("custDetails.dat"); //open file
if (Myfile.is_open())
{
string Line; //declare string to store lines of text
while ( getline( Myfile, Line ) ) //fetch text from file
{
FileVector.push_back( Line );
string FindThis( wait );
if( Line.find( FindThis ) != string::npos )
{
cout << "Found: " << FindThis << endl;
}
else
{
cout << "Did not find " << FindThis << endl;
}
}

Any help with the remove function would be good.
BOB8000

Nov 12 '06 #11

P: n/a
"bob8000" <bo*****@hotmail.co.ukwrote:
Thank you for the help that you have all provided on my vector
problems.

I have almost finish what I need my programme to do. It is now asking
the user for input and then finding what the user requests from the
vector. Excellent.

All I need help on now is how to remove the line of text from the
vector and I will be done.
Try "vector.erase".

--
To send me email, put "sheltie" in the subject.
Nov 12 '06 #12

P: n/a
Thanks for that big suggestion there.

How does that actually work with the code I have.

I am new to c++, I have not got a clue what I am doing

So any examples would be nice.

BOB8000

Nov 13 '06 #13

P: n/a
In article <11**********************@k70g2000cwa.googlegroups .com>,
"bob8000" <bo*****@hotmail.co.ukwrote:
Thanks for that big suggestion there.

How does that actually work with the code I have.
Looking at the code you have so far, you don't need the vector at all.
If "FindThis" is not found, then copy the line to the new file,
otherwise don't.

--
To send me email, put "sheltie" in the subject.
Nov 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.