heloo

Pages: 12
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


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
it is harder for me I'm a beginner
still I do not know well to work with vec

please help me
#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
Then ask questions on what you don't understand, we won't just do your homework for you.

Here are a few links that will be helpful:
read files:
http://www.cplusplus.com/reference/fstream/ifstream/?kw=ifstream
http://www.cplusplus.com/reference/string/string/getline/?kw=getline

string type:
http://www.cplusplus.com/reference/string/string/?kw=string
std::string str = "Hello World!";

vector type:
http://www.cplusplus.com/reference/vector/vector/?kw=vector
std::vector<std::string> vec;

remove unique elements
http://www.cplusplus.com/reference/algorithm/unique/?kw=unique
combine that with vector.erase to get the vector with unique data:
vec.erase(std::unique(vec.begin(), vec.end()), vec.end());

Write to file:
http://www.cplusplus.com/reference/fstream/ofstream/?kw=ofstream

good luck, ask questions if you have some.
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();
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




is not change the order
sorry my mistake
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
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";
    }
}

Text1
Text2
Text3
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";
    }
}

Text3
Text2
Text1


Did you recompile the programm?
What does it print?
Last edited on
I was wrong sorry
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



Yeah, just insert that line you have there before you write your vector to your file...
is not doing enithing...
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
yes
you sure it does not work?

post your code again and please use the code tag (on the right side under Format the <> symbol )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

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");
	fileLines.erase(std::unique(fileLines.begin(), fileLines.end()), fileLines.end());
	if (outFile.is_open())
	{
		
		std::vector<std::string>::iterator rIt;
		for (rIt = fileLines.begin(); rIt < fileLines.end(); rIt++)
		{
			outFile << *rIt << "\r\n";
		}
		outFile.close();
	
	}
	else
	{
		
		std::cout << "Nu pot deschide fisierele output \n";
		return 1;
	}


	

	system("pause");
	return 0;
}
I tried but it does not work
Pages: 12