views:

25

answers:

1
ofstream osCtrs("cts.txt",ios::out);
if (osCtrs.is_open()){
    for(unsigned ci = 0; ci < k; ci++){
        KMpoint& x =  ctrs[ci];
        for (unsigned di = 0; di < dim; di++)
        {
            //osCtrs << x[di];
            osCtrs << "what is happening?";
        }
    }
    osCtrs.close();
}

anything wrong? file is created, but always empty,

A: 

The code works fine for me, given positive values for k and dim. Are you sure they're both non-zero? If either one is 0 or less, the program will never enter the inner loop where you're actually outputting stuff. Try setting a breakpoint and stepping through the code to see what's happening.

Also, you don't need to specify ios::out for an ofstream, it's implied.

tzaman
thank you, i can close this question now,i think it is because this sentence does not work so well,//osCtrs << x[di];at first, "x "is float type, then i should use this :osCtrs << fixed << setprecision(5) << x[di];And maybe the comments does not work well, or the Compliler has memory!!! so even i commented the sentence, it still did not work.
JJgogo
Actually, if `dim` is an `int` smaller than zero, then `di < dim` will work in surprising ways. `dim` will convert to a large unsigned int; on VC++ it would be over 2 billion. The same logic applies to `k`
MSalters