How can I add iteration to my code?

Sep 18, 2019 at 3:28pm
I have a text file like this:

1
2
3
{4 <1,-1,-1> <1,1,-1> <-1,1,-1> <-1,-1,-1>}
{4 <1,3,-1> <1,1,-1> <1,1,0.5> <1,3,0.5>}
{4 <1,3,-1> <-1,3,-1> <-1,1,-1> <1,1,-1>}


and I wrote the bellow code:

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
#include <iostream>     
#include <fstream>      
using namespace std;
int main () {
	ifstream iss("a.txt");     
	char c[256];
	unsigned int curLine = 0;
	int offset;
	string line;
		
	iss.seekg(0,iss.cur);
	iss.ignore(256,'<');
	iss.get(c,256,'>');
	char* search = c;
			
	while(iss.good()) {
		getline(iss,line); 
		curLine++;
		if ((offset = line.find(search, 0)) != string::npos) {
			cout << "found: " << search << " line: " << curLine << endl;
		}
	}		
	iss.close();
	return 0;
}


the above code reads the first bracket (I mean 1,-1,-1) then searches it in whole text file, and giving me this output:

1
2
3
found: 1,-1,-1 line: 1
found: 1,-1,-1 line: 9
found: 1,-1,-1 line: 10



now if I want that the code reads next bracket (i.e. 1,1,-1) I have to manually change program, like 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
#include <iostream>     
#include <fstream>      
using namespace std;
int main () {
	ifstream iss("a.txt");     
	char c[256], c1[256]; //add c1[256]
	unsigned int curLine = 0;
	int offset;
	string line;
		
	iss.seekg(0,iss.cur);
	iss.ignore(256,'<');
	iss.get(c,256,'>');
	

	iss.seekg(0,iss.cur); // newly add
	iss.ignore(256,'<'); //newly add
	iss.get(c1,256,'>'); //newly add
	
	char* search = c1;	
	
	while(iss.good()) {
		getline(iss,line); 
		curLine++;
		if ((offset = line.find(search, 0)) != string::npos) {
			cout << "found: " << search << " line: " << curLine << endl;
		}
	}		
	iss.close();
	return 0;
}


the output is:

1
2
3
4
5
6
7
8
9
found: 1,1,-1 line: 1
found: 1,1,-1 line: 2
found: 1,1,-1 line: 3
found: 1,1,-1 line: 4
found: 1,1,-1 line: 10
found: 1,1,-1 line: 13
found: 1,1,-1 line: 16
found: 1,1,-1 line: 19


my question is: how can I change program to avoid adding manually?
in this way, the code reads each bracket separately and search it in whole text.
Last edited on Sep 18, 2019 at 3:29pm
Sep 18, 2019 at 4:47pm
1. Don't complicate matters by reading from a file.
2. Don't complicate matters by cramming everything into main.

Here, I constructed a function to parse out each token from a given line in memory.
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;

using vecstr = vector<string>;

vecstr splitOutChevrons(const string &line) {
  size_t  pos = 0;
  vecstr result;
  while ( (pos=line.find('<',pos)) != string::npos ) {
    size_t endpos = line.find('>',pos);
    if ( endpos != string::npos ) {
      string token = line.substr(pos+1,endpos-pos-1);
      result.push_back(token);
    } else {
      // broken line, > missing
    }
    pos = endpos;
  }
  return result;
}

int main ( ) {
  string test = "4 <1,3,-1> <-1,3,-1> <-1,1,-1> <1,1,-1>}";
  vecstr result = splitOutChevrons(test);
  for ( auto s : result ) {
    cout << "Token===" << s << "===" << endl;
  }
}

$ g++ -std=c++11 foo.cpp
$ ./a.out 
Token===1,3,-1===
Token===-1,3,-1===
Token===-1,1,-1===
Token===1,1,-1===


From here, it's easy to build this, knowing that you have a tested function already in place.
1
2
3
while ( getline(iss,line) ) {
    result = splitOutChevrons(line);
}
Sep 19, 2019 at 8:49am
dear "salem c" so many thanks for your answer.
Topic archived. No new replies allowed.