-5

In this piece of code, in every iteration of while loop, a line is read from a file. The line is something like:

13,4636137,29464742,29464746,995560164

for every number specified in bold, the existence of that number as a key in a std::map is checked in a for loop. If the key exists, then the value is appended to the string. Then the string is written to the file.

If one of keys not exist in the map, then for that line nothing should be written to the file. (bool is_points_in_range)

But in practice, in the cases that the last point (outside of for loop) is not in key list, the program logic performs well.

Why the boolean operator does not change in for loop?

vector<string> res;
ifstream infile;
map<string, string> m;
while(infile.getline(buffer, LINE_BUFFER_LEN))
{
    line=string(buffer);
    res = SplitBySep(line, ",");
    bool is_points_in_range=true;
    string geo_file_line;
    for (int i=2;i<res.size()-1;i++){
        if ( m.find(res[i]) == m.end() ) {
            is_points_in_range=false;
            break;
        } else {
            geo_file_line= geo_file_line.append(m[res[i]]).append("^");
        }   
    }
    if ( m.find(res[res.size()-1]) == m.end() ) {
        is_points_in_range=false;    
    } else {
        geo_file_line= geo_file_line.append(m[res[res.size()-1]]).append("\n");
    }
    if (is_points_in_range){
        fprintf(fp_geo, "%s",geo_file_line.c_str());
    }
}
  • 2
    Have you tried debugging the program? What is `res`? You are reading from infile but nowhere in your code do you process `buffer` or `line` so how are you expecting this to work? – Excelcius May 19 '15 at 10:40
  • I editted the code, I read from file, then tokenize it with a function that I know that works well. – user2949310 May 19 '15 at 10:50
  • btw it's very hard to read your code – Quest May 19 '15 at 10:53
  • Ok, the code makes more sense now. Is this all there is to it or is still some part of the code missing? Because at the moment it simply looks like `m` is never populated with any values. It starts empty, no values can be found, and no values are added in case nothing is found. – Excelcius May 19 '15 at 10:54
  • m has values. Some of keys are found and the else part executed in if. But for the first key, even when that key is not exist in m, the boolean is_points_in_range does not change to false and so the condition of last if remains true. and the line is written in output file. – user2949310 May 19 '15 at 11:01
  • Please read [ask]. If nothing else, your title is extremely uninformative. – dandan78 May 19 '15 at 11:08
  • If this is the code that is executed it either has to find a matching value in `m` or `is_points_in_range` will be set to false. There is no other alternative, unless I'm missing something. So I guess something is missing in this question. I only see one option: You have to debug your program! – Excelcius May 19 '15 at 11:12
  • 1
    Editing you code following the suggestions in the answers actually invalidates this answers. Other users will not be able to relate the answer to your question if they do not trace the full history of the post. IMHO it would be even better to post your corrections as an answer instead of improving the code in your question. – 463035818_is_not_an_ai May 19 '15 at 11:18
  • @tobi303 I generally agree with you, but IMO the 2 answers should not have been posted before the edits in the first place. Both were posted at a time when `m` and `res` were not even defined or populated anywhere in the code. – Excelcius May 19 '15 at 11:26
  • 1
    @Excelcius I get your point. It is a bit like with the egg and the chicken: maybe the OP could have spend a bit more time before posting not to have too obvious errors in the code.... – 463035818_is_not_an_ai May 19 '15 at 12:53

3 Answers3

2

res.size()-1 is extremely dangerous. res.size() is an unsigned type, and subtracting one from it when res.size() is 0 is going to give you a very large unsigned integer due to wraparound.

So your program is essentially undefined when res is unpopulated.

I'm amazed your compiler didn't warn you. Do you have warnings switched off?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

It looks like m.find(res[i]) == m.end() is never true. Are you sure that this table is fulled properly? res[i]

Jade
  • 1
  • 2
0

It is better to ask that why this conditional statement does not work well? We know that for obtaining that a key exists in a std::map, we can use:

if ( m.find("f") == m.end() ) {
    // not found
} else {
    // found
}

as indicated in this question.

But for

map<string, string> m;

this statement does not work well. I dont have any 29464742 key (I am sure), but when I search for this key the else part is executed. Also when I use m.count('29464742') , 1 is returned. I don't know why?

I corrected my code with this conditional statement:

if ( m["f"] == "" ) {
    // not found
} else {
    // found
}

This works for me and my problem is solved.

Community
  • 1
  • 1
  • Honestly, I think you have no idea what's going on in your code. You really should learn how to debug and have a look at the content of the map before execution of the condition ("I am sure" doesn't sound like good proof to me). You just replaced it with an arbitrary condition taken from another SO question which will always be true. If this is what you are after you can always get rid of the `if` statement completely. – Excelcius May 19 '15 at 20:26