end line

I split a line into token. I have to compare with other word, but I notice that when I arrive at the end of the line, last token is for example:

endline\000\000 ecc..

Is there a way to eliminate this terminal char?

I think more information is needed here. C-style strings are always supposed to be null-terminated. If you're reading past the null-termination, you're potentially reading into illegal memory. If you're converting things into std::strings, show us how you're doing that.

Show us exactly how you are splitting the line and what the data in and out is.
Last edited on
it may be easier to code and more effiicent to strstr() or find() the compare word against the long string than to try to do it one by one over a tokenized group. Go back before you tokened may be better.
you can of course replace all 0 with space except the last one, in a copy. This should give you back the same thing you had before you tokened it, so that seems a bit like too much work somewhere (as above).

string ignores zeros entirely; it operates off the size variable alone.
Last edited on
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
string line;
string word;
myfile in;
int i=0;
bool flag=false;
myfile.getline(in,line);
while(i<line.size()){
        word.clear();
        if (isspace(line[i])){
            while(isspace(line[i])) i++;
        }
        if(ispunct(line[i])){
            if (line[i]=='('||line[i]==')') {
                     word += line[i];
                     flag = true;
            }else{
                    cout<<"error'"<<endl;
                    return;
                }
        }
        if(!flag) {
            while (!ispunct(line[i]) && !isspace(line[i])) {
                word += line[i];
                i++;
            }
            if(ispunct(line[i])){
                if (!(line[i]=='('||line[i]==')')){
                    cout<<"error"<<endl;
                    return ;
                }
            }
        }
        i++;
//compare
}
//other things 
Last edited on
Reading line:
a and b or c

for each iteration, string word will be:
a
and
b
or
c\000\000
Last edited on
I get "error" printed when I modified your code to compile (and I used cin as input).

However, when I change your line[i] calls to line.at(i), an std::out_of_range exception was thrown. I didn't look hard enough to know where exactly you're going out of bounds, but you definitely are somewhere.

I suggest adding more bounds checks in your code. For example, in your loop on lines 22-25, you keep incrementing i when line[i] is NOT punct AND is NOT space. What happens if the last character is 'a'?

At some point, you're incrementing i past the bounds of your array before the next iteration of the outer loop.
Last edited on
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
getline(_my_file,line);
    while(i<line.size()){
        word.clear();
        if (isspace(line.at(i))){
            while(isspace(line.at(i))) i++;
        }
        if(i<line.size())
        if(ispunct(line.at(i))){
            if (line.at(i)=='('||line.at(i)==')') {
                     word += line.at(i);
                     flag = true;
            }else{
                    cout<<"error'"<<endl;
                    return ;
                }
        }
        if(!flag) {
            while (!ispunct(line.at(i)) && !isspace(line.at(i))) {
                word += line.at(i);
                i++;
                if(i==line.size()) break;
            }
            if(i<line.size())
            if(ispunct(line.at(i))){
                if (!(line.at(i)=='('||line.at(i)==')')){
                    cout<<"error'"<<endl;
                    return ;
                }
            }
        }
        i++;
        flag=false;
}


this way could be good?
Last edited on
Topic archived. No new replies allowed.