writing to file _sometimes_ goes wrong

Hi,

I'm baffled by the output of my program. It usually generates neat output, but occasionally prints characters twice, or forgets them, or does other nasty things like that.
The output looks like this:


//snip
1.41179e-008	0.152256	9.99992e-010	359.839	-3.31245e-014	73413.1
1.41079e-008	0.152506	9.99992e-010	359.834	-3.31702e-014	73413.1
11.40979e-008	0.152756	9.9999e-010	359.849	-3.32182e-014	73413.1
1.1.40879e-008	0.153006	9.9999e-010	359.846	-3.32666e-014	73413.1
1.1.40779e-008	0.153256	9.99991e-010	359.841	-3.33125e-014	73413.1
1.1.40679e-008	0.153506	9.99992e-010	359.836	-3.33614e-014	73413.1
1.1.40579e-008	0.153756	9.99991e-010	359.845	-3.34095e-014	73413.1.41.40479e-008	0.154006	9.9999e-010	359.846	-3.34559e-014	73413.1.41.40379e-008	0.154256	9.99991e-010	359.841	-3.35054e-014	73413.1.41.40279e-008	0.154506	9.99992e-010	359.837	-3.35532e-014	73413.1.41.40179e-008	0.154756	9.99991e-010	359.842	-3.36003e-014	73413.1.41.40079e-008	0.155006	9.9999e-010	359.848	-3.36503e-014	73413.1.31.39979e-008	0.155256	9.99991e-010	359.843	-3.36978e-014	73413.1.31.39879e-008	0.155506	9.99992e-010	359.838	-3.37459e-014	73413.1.31.39779e-008	0.155756	9.99991e-010	359.838	-3.37961e-014	73413.1.31.39679e-008	0.156006	9.9999e-010	359.849	-3.38434e-014	73413.1.31.39579e-008	0.156256	9.9999e-010	359.844	-3.38924e-014	73413.1.31.39479e-008	0.156506	9.99992e-010	//snip
22.60742e-009	0.440018	9.91355e-010	352.224	-1.21087e-012	73413.12.2.59742e-009	0.440268	9.91182e-010	352.119	-1.22243e-012	73413.12.2.58742e-009	0.440518	9.90953e-010	352.013	-1.23416e-012	73413.12.2.57742e-009	0.440768	9.90718e-010	351.906	-1.24607e-012	73413.12.2.56742e-009	0.441018	9.90434e-010	351.814	-1.25815e-012	73413.12.2.55742e-009	0.441268	9.90176e-010	351.706	-1.27042e-012	73413.12.2.54742e-009	0.441518	9.89917e-010	351.592	-1.28287e-012	73413.12.2.53742e-009	0.441768	9.89668e-010	351.476	-1.2955e-012	73413.2.52.52741e-009	0.442018	9.89429e-010	351.359	-1.30833e-012	73413.2.52.51741e-009	0.442268	9.89146e-010	351.24	-1.32136e-012	73413.12.2.50741e-009	0.442518	9.8881e-010	351.136	-1.33459e-012	73413.1
22.49741e-009	0.442768	9.885e-010	351.015	-1.34802e-012	73413.1
2.48741e-009	0.443018	9.88189e-010	350.889	-1.36166e-012	73413.1
2.47741e-009	0.443268	9.87887e-010	350.761	-1.37551e-012	73413.1
2.46741e-009	0.443518	9.87594e-010	350.631	-1.38957e-012	73413.1
//snip


This is somewhere in the middle of the output file. The file is 2850 lines, and most of it looks just fine. Here however we see that it forgot a whole bunch of "endl"'s, printed some 1.4... as 1.1.4... or 2.3 as 22.3.

The code is just
1
2
3
ofstream out2[4];
//snip
out2[i] << z << "\t" << tt << "\t" << TA[i][0] << "\t" << TA[i][1]*180/pi << "\t" << ta[i][2] << "\t" << ft << endl;


What's going wrong here?
Last edited on
Just wondering, did u try to replace endl with "\n" ?
endl flushes to disk
IMO u should be using "\n" anyway if u'r going to be making tons of these calls. I don't know if that will fix ur problem though...
Are you doing any multithreading? That's the only way I could see something like this happening.

The << operator is fundamentally not thread safe.
Last edited on
Thanks for the suggestions.

I changed the endl with \n. Didn't help unfortunately.

No multithreading. The pc I'm working on is an old single core...
The number of cores doesn't really have anything to do with it.

Any chance I can see the full code?
1
2
3
ofstream out2[4];
//snip
out2[i]
Do any of these streams refer to the same file or do you have four distinct files?
Do any of these streams refer to the same file or do you have four distinct files?


Yes, different files. 3 different files to be precise. Apparently, 2 files are given the same name. fixed now, Thanks a lot!
Just wondering, did u try to replace endl with "\n" ?
endl flushes to disk
IMO u should be using "\n" anyway if u'r going to be making tons of these calls. I don't know if that will fix ur problem though...


Anyway, one doesn't use
"\n"
but
'\n'
.
endl can reduce the streams speed (especially in file-streams). Don't use it if you don't want to flush the stream.

Apparently, 2 files are given the same name.


Hows that possible?
Last edited on
without seeing ur coding, and knowing ur exact need, i cant help u.

its ur coding problem, i think u might, did mistake in reading, saving, clearing the string.
check ur coding part .
Topic archived. No new replies allowed.