read and process a paragrpah

Hi,

I would like to read in a text file paragraph by paragraph, processing each paragraph along the way. Here is some example data

# comment
s test1 XYZHDFSSFJ
s test2 XYDSFGJSFJ
s test3 XYZShgjhjF
s test4 XYZSHJKDHF

# comment
s test1 XYZSFJ
s test2 XYMSFJ
s test3 XYZSGJ
s test4 XYZJFJ



Here is a snippet of my code;

do{
getline(file,line);
for(i=0;i<=3;i++){
getline(file,line);
process(line)
}
}while (line.length() > 0);


First, Im not sure if this is the right way to read the paragraphs, and secondly, what is the best way to break up the line according to the variables? I have done this using fscanf in C but I hear that is no good and should not be done in c++.

Thanks in advance.

How do you define a paragraph? You sample data doesn't conform with a normal definition of a paragraph.

I'd say a paragraph is a string of one or more sentences, and a sentence as a string of words seperated by spaces and termiated by '.'

You need to be clear on your definition to proceed.
Sorry, by paragraph I mean an empty line separating the two blocks of lines - the blocks of (non-empty) lines are the paragraph. So In my example there are 2 paragraphs.
kbw -- his definition of a paragraph is familiar to people familiar with some old *nix tools... like troff and the like.

How about something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <ciso646>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef vector <string> paragraph_t;

istream& operator >> ( istream& ins, paragraph_t& paragraph )
  {
  string line;
  paragraph.clear();
  while (getline( ins, line ) and !line.empty())
    {
    paragraph.push_back( line );
    }
  return ins;
  }

An example of use:
1
2
3
paragraph_t p;
cout << "Please enter some lines of text. Hit ENTER twice to end.\n";
cin >> p;

Hope this helps.
Thanks for your help, not sure if that helps though. Basically, for each paragraph I need an array of strings for the "tests"

array1 = ["test1","test2","test3","test4"]

and an array of strings for each string after each test,

array2 = ["XYZHDFSSFJ","XYDSFGJSFJ","XYZShgjhjF","XYZSHJKDHF"]

maybe an associative array combining both if possible. But the point is I need to process these arrays, output a single string, then move on to the next "paragraph" (block of text).

Hope this clears up my problem.
So, I think I understand Duoas' sulotion. I can now read paragraphs using the redefined >> operator

1
2
3
4
5
6
7
8
9
10
11
blockcount=0;

if(infile.is_open())
   {
      while(infile >> p)
      {
          blockcount++;
      }
     }
   else cout << "Unable to open inputfile " << inname <<endl;
cout<<"There are " << blockcount <<" paragraphs" << endl;



Any ideas on how to make an associative array from elements in the paragraph (as discussed in above post)?

Thanks again.
Topic archived. No new replies allowed.