I am trying to write a program that will read a web log file and parse the results into an array of each line. The log does not have any line breaks, but every entry ends with )" so I want to read the log file until I reach those two characters, )", and then go to the next line. This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void myclass::lineNum( int i )
{
i=0;
std::ifstream in;
while (getline(in,line[i])) // This is saying while
{ // ifstream can be read, right?
if (in.fail()) break;
if (in)
getline(in, line[i],')"');// Clearly, it wants a char as the
i++; // delimiter but I need it to use
count++; // the string ")""
}
}
#include <iostream>
#include <string>
std::string parseLine(const std::string& myLine)
{
std::string delim(")\"");
size_t index = myLine.find(delim);
return myLine.substr(0, index);
}
int main()
{
std::string myLine("This is the line i need to grab)\"Some extra crap i don't want.");
std::cout << "line: " << parseLine(myLine) << std::endl;
return 0;
}
Hey, thanks for your reply! So, I'm a bit novice at C++. Would it make sense, using your example, to first put the whole web log file into a string, and then use the code you suggested? I don't know how else to do it, but it is 184 pages when I paste it into Microsoft Word. That seems like bad practice to store all of that in a single string, no?
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
// 'line' is now the one line from your file, so you could call that function here e.g.
string parsedLine = parseLine(line);
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
what's a web log file look like? is it one block of text or does it contain lines?
Well, I thought it was just a stream of endless text with no new lines, but when I pasted a sample into here, it separated the lines neatly, when I previewed my post. I tried it in Microsoft Word as well, and the lines had breaks at the end of each entry. For some reason, notepad displays it without line breaks.
So, I don't think I need to find the )" string anymore. Now, I'm just having a problem with this causing my program to crash:
1 2 3 4 5 6 7 8 9 10 11 12
void myclass::lineNum( int i )
{
i=0;
std::ifstream in;
while (getline(in,line[i]))
{
if (in.fail()) break;
getline(in, line[i]);
i++;
count++;
}
}
So, I don't think I need to find the )" string anymore
well that's 10 minutes of my life i won't get back :)
1. If you're passing in i to your function, why are you then initialising it to zero? in other words why are you passing anything in?
2. count isn't doing anything.
3. that link i pasted in (http://www.cplusplus.com/doc/tutorial/files/) shows how to read in a file line by line.
4. where are you actually opening the file to read?
1. This is for an assignment, and we are required to have a member function with an integer passed into it.
2. There is another function that needs to display the number of total lines and I was planning to use count for that.
3. Yeah I think I actually am getting it now which is really exciting. I got it to count all the lines and even give me back lines based on user entry. This is cool.
4. I have a separate function to open the file (as per the assignment instructions - this part seemed odd to me).