file processing

hey everyone,

i am having trouble with this project. I have gotten off to a small start, but don't really know if i am in in or going in the right direction, anything helps!!

Project:

For this project, download the text file weblog.txt
Note: To download this file, right click on the link and select SAVE AS or SAVE TARGET AS

This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request to the web server to view a web page, which is a file residing on the server. Each time a page is requested by a browser, the web server records information about that request. This weblog.txt holds the details of some of those requests. See below for a list of fields with an example:

Web Log Example

This file does not include all possible information that could be collected by a web server. The full description of the apache log file format can be found here:http://httpd.apache.org/docs/2.2/logs.html (Links to an external site.)Links to an external site.

For this project, you can use each line of the web log file as one string using the string class' getline function.

Minimum Requirements:

Create a function to read each line of the web log file (3 points).
Each line should then be stored in an array (your choice) such that the first element of the container is the first line of the web log file. Because each element will hold an entire line from the file, the container you choose should be declared with a data type of string (3 points). Note: You must declare the array in a function (main function or any other function). Declare the array with a size of 3000.
Create another function to write the first 10 lines in the container to a file called topTen.txt.
Create another function that will accept an integer line number in the parameter list and string return value. When the function is called in main, a line number must be passed to the function. The function should then return the line from the container that corresponds to that line number. You must pull the line from the container, not from the file. Because you are required to pull the line from the container, you must include the container in the parameter list of this function and pass the container to this function when it is called by main. The main function should then display the string that is returned by the function.



my code so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void myLine(){

string line;
string myArray[3000];
int numuse = 0;

ifstream myFile("weblog.txt");

if (myFile.is_open()) {

while (getline(myFile, line)) {

myArray[numuse++] = line;

}
myFile.close();
cout << myArray[3000];
} else {
cout << "File not found";
}
}

int main(){

myLine();

}
Play around with this...
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void myLine() {

	string line;
	string myArray[3000];
	int numuse = 0;

	ifstream myFile;
	myFile.open ("weblog.txt");

	if (myFile.is_open()) {
		
		while (getline(myFile, line)) // To get you all the lines.
		{
		
			myArray[numuse] = line;
			numuse ++ ;
		}

		myFile.close();
		for (int i = 0; i < 4; i++) {
			cout << myArray[i] << "\n";
		}
	}
	else {
		cout << "File not found";
	}
}

int main() {

	myLine();

	cin.get();
	return 0;
}
it works perfectly, but how do i get the last part of the assignment?
Welcome to the forum!

When posting code, please use code tags. Highlight the code and click the <> button to the right of the edit window.

You're off to a good start. Since myArray needs to be passed around to different functions, I think you need to declare it in main(). That means you'll need to declare numuse there too.

Hmm. Consider changing those names to something more descriptive like lines and numLines.

1
2
string lines[3000];   // lines in the file
unsigned numLines;   // number of lines read. 


Then I'd rename myLines() to readLines() and have it do this:
1
2
3
// Read lines from input file into lines array. Return the number
// of lines read.
unsigned readLines(string lines[])

It will return 0 if there's an error.

The other two functions might look like this:
1
2
3
4
5
6
7
8
// Write the first 10 lines to cout
void writeFirst10(string lines[])
...

// Given a line number (1, 2, 3, ...)
// return the line.
string getLine(string lines[], unsigned lineNum)
...

I agreed with dhayden about getting a bit more organized.
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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


void readFile(string myArray[]) {

	string line;
	int numuse = 0;

	ifstream myFile;
	myFile.open ("weblog.txt");

	if (myFile.is_open()) {
		
		while (getline(myFile, line)) // To get you all the lines.
		{
		
			myArray[numuse] = line;
			numuse ++ ;
		}

		myFile.close();
	}
	else {
		cout << "File not found";
	}
}

void writeFile(string myArray[]) {

	ofstream myFile;
	myFile.open("topTen.txt", ios::out); //opens file. if no file we create one here.

	for (int i = 0; i < 10; i++) {
		myFile << myArray[i] << "\n";
	}
	myFile.close();
}

string getLine(string myArray[], unsigned number) {
	return myArray[number -1];
}

int main() {

	string myArray[3000];

	readFile(myArray);
	for (int i = 0; i < 10; i++) {
		cout << myArray[i] << "\n";
	}

	writeFile(myArray);

	cout << endl;
	cout << getLine(myArray, 3);

	cin.get();
	return 0;
}
Topic archived. No new replies allowed.