Jul 3, 2015 at 9:38am UTC
Heloo
I have a problem
i have two input files file1.txt and file2.txt and I need
In an outputfile.txt to show all the lines that appear in both input files .
if I have in file1.txt
Line A
Line C
Line D
in the second file2.txt
Line B
Line C
Line D
Line E
the output file to appear :
Line C
Line D
thanks
Jul 3, 2015 at 9:44am UTC
make a std::vector<std::string>
read file1 to vec
read file2 to vec
remove unique items
write vec to file
Alternatively show us what you have so we can help you
no problem
Last edited on Jul 3, 2015 at 9:45am UTC
Jul 3, 2015 at 9:48am UTC
it is harder for me I'm a beginner
still I do not know well to work with vec
please help me
Jul 3, 2015 at 9:51am UTC
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> fileLines;
std::string currLine;
std::ifstream inFile("marius.txt");
std::ifstream File("marius1.txt");
if (inFile.is_open())
{
while (inFile.good())
{
std::getline(inFile, currLine);
fileLines.push_back(currLine);
}
inFile.close();
}
else
{
return 1;
}
if ( File.is_open())
{
while (File.good())
{
std::getline(File, currLine);
fileLines.push_back(currLine);
}
File.close();
}
else
{
return 1;
}
std::ofstream outFile("marius2.txt");
if (outFile.is_open())
{
std::vector<std::string>::reverse_iterator rIt;
for (rIt = fileLines.rbegin(); rIt < fileLines.rend(); rIt++)
{
outFile << *rIt << "\r""\n";
}
outFile.close();
}
else
{
std::cout << "Nu pot deschide fisierele output \n";
return 1;
}
system("pause");
return 0;
}
sorry because of my bad English I am from ROmania
They are reversed but I want to be read in order
how put the line in order?
Last edited on Jul 3, 2015 at 9:58am UTC
Jul 3, 2015 at 10:05am UTC
how put the line in order?
std::ofstream outFile("marius2.txt");
if (outFile.is_open())
{
std::vector<std::string>::reverse_iterator rIt;
for (rIt = fileLines.rbegin(); rIt < fileLines.rend(); rIt++)
{
outFile << *rIt << "\r""\n";
}
outFile.close();
Jul 3, 2015 at 10:10am UTC
how put the line in order?
use an iterator, not a reverse_iterator:
and use begin and end instead of rbegin and rend
Also: use "\r\n" instead of "\r""\n" (which should not even work)
1 2 3 4 5
std::vector<std::string>::iterator rIt;
for (rIt = fileLines.begin(); rIt < fileLines.end(); rIt++)
{
outFile << *rIt << "\r\n" ;
}
Also, you don't have unique values yet
Last edited on Jul 3, 2015 at 10:12am UTC
Jul 3, 2015 at 10:21am UTC
further I must use this command
vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); for remove unique elements
if I have in file1.txt
Line A
Line C
Line D
in the second file2.txt
Line B
Line C
Line D
Line E
the output file to appear :
Line C
Line D
in output file I must heve only the duble words D and C
Last edited on Jul 3, 2015 at 10:22am UTC
Jul 3, 2015 at 10:24am UTC
It does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example program 1
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> texts = { {"Text1" }, {"Text2" }, {"Text3" } };
std::vector<std::string>::iterator rIt;
for (rIt = texts.begin(); rIt < texts.end(); rIt++)
{
std::cout << *rIt << "\r\n" ;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Example program 2
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> texts = { {"Text1" }, {"Text2" }, {"Text3" } };
std::vector<std::string>::reverse_iterator rIt;
for (rIt = texts.rbegin(); rIt < texts.rend(); rIt++)
{
std::cout << *rIt << "\r\n" ;
}
}
Did you recompile the programm?
What does it print?
Last edited on Jul 3, 2015 at 10:25am UTC
Jul 3, 2015 at 10:28am UTC
further I must use this command
vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); for remove unique elements
if I have in file1.txt
Line A
Line C
Line D
in the second file2.txt
Line B
Line C
Line D
Line E
the output file to appear :
Line C
Line D
in output file I must heve only the duble words D and C
Jul 3, 2015 at 10:33am UTC
Yeah, just insert that line you have there before you write your vector to your file...
Jul 3, 2015 at 10:41am UTC
did you replace all the "vec" with "fileLines"?
in your case it should look like this:
fileLines.erase(std::unique(fileLines.begin(), fileLines.end()), fileLines.end());
Last edited on Jul 3, 2015 at 10:43am UTC
Jul 3, 2015 at 10:45am UTC
you sure it does not work?
post your code again and please use the code tag (on the right side under Format the <> symbol )
Jul 3, 2015 at 10:59am UTC
I tried but it does not work