![Avatar of Ryanbhl](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
Ryanbhl
asked on
C++ help Please
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;
}
************************** ********** ********** ***
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;
}
**************************
![Avatar of Ryanbhl](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
![Avatar of Ryanbhl](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
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.
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.
![Avatar of rstaveley](https://filedb.experts-exchange.com/files/public/2013/11/18/9b84c6b1-b43b-484f-86f9-ca2dbf3dce69.png)
> (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.
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.
![Avatar of meow00](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
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
"(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
![Avatar of Ryanbhl](https://cdn.experts-exchange.com/images/experts-exchange/avatar-01-large.gif)
ASKER
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
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
--------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
}
// Read the strings from the file
{
std::ifstream fin("strings.dat");
std::string str;
while (getline(fin,str))
std::cout << str << '\n';
}
}
--------8<--------