I want to "getline" until I reach a set of TWO characters

Hi, thanks for reading.

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 ")""
    }
}


Thanks very much for the help!
hiya,
The clever people on this forum will probably have a more efficient way, but i've just cobbled this together:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#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;
}
Last edited on
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?
you want to call that parseLine ater you've read one line from your file.

so using the example from here:
http://www.cplusplus.com/doc/tutorial/files/

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
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace 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?
Last edited on
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++;
    }
}
Last edited on
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?
Sorry. I'm really bad at this stuff.

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).
Topic archived. No new replies allowed.