?
Solved

can't see vector problem [too many trees]

Posted on 2003-03-27
3
Medium Priority
?
255 Views
Last Modified: 2010-04-01
i have a text file search where i store the results in a vector which is
initialised in main 'vector<char *> results;'


     //print the string    
          memset (printOut,' ',5);
          cout << printOut << endl;    
                         
     //add string to results vector
          vector<char *>::iterator iter = results.end();
          results.insert(iter, printOut);

after the search is complete i try to save the results to a file from a different function.
i pass the vector by reference: 'void saveResults(vector<char *> &results)'

if (outfile.is_open())
          {
              outfile << "search results:" << "\n";
          vector<char *>::iterator iter = results.begin();
                         
               for (iter = results.begin(); iter < results.end(); iter++)
               {
               outfile << *iter << "\n";     //output each element of the vector
               }
          outfile.close();


the output is blank both onscreen and in the file apart from the carriage returns.
i have checked the contents of the vector immediately after assigning the strings and it returns the strings no problem.

?
0
Comment
Question by:antonumia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 8218945
Hmm, it should be

results.insert(iter, strdup (printOut));

or just

results.push_back ( strdup (printOut));

Otherwise (as you are using a vector of pointers), you will simply end up adding the same pointer to the list each time and as you are changing teh memory it points to...

BTW: Why not using a 'vector<string>'?
0
 
LVL 1

Author Comment

by:antonumia
ID: 8218971
great,
that works fine.
is a vector<string> better?
0
 
LVL 86

Expert Comment

by:jkr
ID: 8219002
>>is a vector<string> better?

Sure - if you 'strdup()' a char*, you have to deallocate the vector entries when you no longer need them. a vector of strings takes care of teh on its own:

string str = printOut;

results.push_back ( str);



0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question