Help opening a file

I am writing a program that takes a weblog, sorts the lines numerically, collects the unique IP addresses, asks the user for a specific IP address, and then shows all the page requests for that specific IP address. I built in my own error messages to let the user know when a file could not be opened. When I run the program, I get my error message for the second time SortedWeblog.txt is opened and i don't know why. if anyone can help, it would be appreciated.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <fstream>

void SortFile(std::vector<std::string>& sortedVector);
void GetIPs(std::vector<std::string>& IPaddress);
void Search(std::string& IP);
void DisplayRequests(std::string& IP);
std::vector<std::string> Lines;
std::vector<std::string> IPs;
std::string IPaddress;

int main()
{
	SortFile(Lines);
	GetIPs(IPs);
	Search(IPaddress);
	DisplayRequests(IPaddress);
	system ("pause");
	return 0;
}

void SortFile(std::vector<std::string>& sortedVector)
{
	std::string line;
	std::fstream weblog;
	weblog.open("weblog.txt");
	if(weblog.is_open())
	{
		while(std::getline(weblog, line));
		{
			sortedVector.push_back(line);
		}
	}
	else std::cout << "Could not open weblog.txt" << std::endl;
	weblog.close();
	sort(sortedVector.begin(), sortedVector.end());
	std::ofstream sortedWeblog;
	sortedWeblog.open("SoredWeblog.txt");
	if(sortedWeblog.is_open())
	{
		for(int i = 0; i < sortedVector.size(); i++)
		{
			sortedWeblog << sortedVector[i] << "\n";
		}
	}
	else std::cout << "Could not open SortedWeblog.txt (1)" << std::endl;
	sortedWeblog.close();
}

void GetIPs(std::vector<std::string>& IPaddress)
{
	std::string line;
	std::string IP;
	std::fstream sortedWeblog;
	sortedWeblog.open("SortedWeblog.txt");
	if(sortedWeblog.is_open())
	{
		int i = 0;
		while(std::getline(sortedWeblog, line))
		{
			IP = line.substr(0,14);
			if(IP != IPaddress[i-1])
			{
				IPaddress.push_back(IP);
			}
			i++;
		}
	}
	else std::cout << "Could not open SortedWeblog.txt (2)" << std::endl;
	sortedWeblog.close();
	std::ofstream IPaddresses;
	IPaddresses.open("IPaddresses.txt");
	if(IPaddresses.is_open())
	{
		for(int j = 0; j < IPaddress.size(); j++)
		{
			IPaddresses << IPaddress[j] << "\n";
		}
	}
	else std::cout << "Could not open IPaddresses.txt" << std::endl;
	IPaddresses.close();
}

void Search(std::string& IP)
{
	std::cout << "Enter an IP address that you would like to see the page requests for." << std::endl;
	std::cin >> IP;
}

void DisplayRequests(std::string& IP)
{
	std::fstream Weblog;
	Weblog.open("weblog.txt");
	if(Weblog.is_open())
	{
		std::string line;
		std::string searchIP;
		while(std::getline(Weblog, line))
		{
			searchIP = line.substr(0,14);
			if(IP == searchIP)
			{
				std::cout << line << std::endl;
			}
		}
	}
	else std::cout << "Could not open weblog.txt" << std::endl;
}
Inconsistent filename here: sortedWeblog.open("SoredWeblog.txt");

By the way, this line should not end with a semicolon, as it will give a valid but empty loop.
while(std::getline(weblog, line));
Last edited on
Topic archived. No new replies allowed.