Solved

C++ help Please

Posted on 2003-12-05
7
349 Views
Last Modified: 2010-04-02
Hi experts,

I have the code as following. Can anyone give an idea how to place two strings in the file into two vectors ?
Thank you very much!

*****************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>

using namespace std;

 #define size 1000

int main()
{
             
      ofstream SaveString("strings.dat", ios::out);
      SaveString << "Test string1\n";
      SaveString << "Test string2\n";
      SaveString.close();


      ifstream Openstring("strings.dat", ios::in);
      char str;
      
      while (!Openstring.eof())
      {
            Openstring.get(str);
            cout << str;
      
      }

      Openstring.close();

/      return 0;
}
*************************************************
0
Comment
Question by:Ryanbhl
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9887684
Not sure if I understood your question properly, but does this help?

--------8<--------
#include <iostream>
#include <fstream>
#include <iterator>
#include <vector>
#include <string>

int main()
{
        std::vector<std::string> v;

        // Load the vector - use push_back to grow it accordingly      
        v.push_back("Test string 1");
        v.push_back("Test string 2");

        // Load the strings into the file
        {
                std::ofstream fout("strings.dat");
                copy(v.begin(),v.end(),std::ostream_iterator<std::string>(fout,"\n"));
        }

        // Read the strings from the file
        {
                std::ifstream fin("strings.dat");
                std::string str;
                while (getline(fin,str))
                        std::cout << str << '\n';
        }
}
--------8<--------
0
 

Author Comment

by:Ryanbhl
ID: 9888598
Hi  rstaveley,

Thank you very much for your help.

May be I did make it clear enough. I have a file stores two strings above, now I want to read the file, take two the strings and put them into two seperate vectors.

Example:
I have two vectors : vec1, vec2
Now, I want to put "Test string 1" into "vec1" ; and "Test string 2" into "vec2" by reading them from the file.
0
 
LVL 1

Accepted Solution

by:
meow00 earned 100 total points
ID: 9888885
Hi,
 
     How about the following code ? ( PS. you might want to change #include <string> to #include <cstring> ... depends on ur compiler )

meow.
---------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

#define size 1000

int main()
{

     ofstream SaveString("strings.dat", ios::out);
     SaveString << "Test string1\n";
     SaveString << "Test string2\n";
     SaveString.close();

     ifstream Openstring("strings.dat", ios::in);
     string temp ;
     int count = 0 ;
     vector<string> vec1, vec2 ;

     while (!Openstring.eof())
     {
        getline(Openstring,temp) ;
        (count%2 ==0)? vec1.push_back(temp): vec2.push_back(temp) ;
        count ++ ;
     }

     Openstring.close();

     cout << "print vec1() :" << endl ;
     for(vector<string>::const_iterator it1 = vec1.begin(); it1 != vec1.end(); ++it1)
        cout << *it1 << endl ;

     cout << "print vec2() :" << endl ;
     for(vector<string>::const_iterator it2 = vec2.begin(); it2 != vec2.end(); ++it2)
        cout << *it2 << endl ;

     return 0;
}

-------------------------------------------------------
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:Ryanbhl
ID: 9890978
Thank you meow00,

Yes, that is what I am trying to do right now.
May I ask a question ?  

******************************************************
(count%2 ==0)? vec1.push_back(temp): vec2.push_back(temp) ;
******************************************************
In the line above, what does "count%2 == 0" mean ?? I am have some confuse here. I am very glad if you can explain it to me.

Again, Thank you very much for your guide.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 9891466
> (count%2 ==0)? vec1.push_back(temp): vec2.push_back(temp) ;

This is equivalent to

    if (count % 2 == 0)             /* If count is an even number */
        vec1.push_back(temp);
    else
        vec2.push_back(temp) ;

It pushes the tring alternately into one vector and then the other.
0
 
LVL 1

Expert Comment

by:meow00
ID: 9892257
Yes, rstaveley is correct !

 "(statement) ? A : B ;" means :
  if statement is true, we execute A ;
  if statement if false, we execute B ;
--------------------------------------------
 Also,  % means the residual of the division (modulus).
 e.g.    7/3 = 2 ......1   -> 7%3 = 1
           8/2 = 4 ......0   -> 8%4 = 0
           9/5 = 1 ......4   ->  9%5 = 4
                             ^
                          This is the residual
0
 

Author Comment

by:Ryanbhl
ID: 9893119
Thank you rstaveley and meow00 a lot,

Now I understand a lot more and know how to use the statement:  "(statement) ? A:B . I was doubt about this before but not anymore.
I really apreciate you guys.

Ryanbhl
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 64
Find Visual Studio Tools 2 78
Unresolved External Symbols 3 55
Best book to learn C++ 4 55
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now