Hi all,

I'm having problems writing to a file, and can't figure out what's happening.

Basically I have a program which tracks movement over several timeunits (each timeunit takes several timesteps). At each timeunit I write the results to a .txt file. However my program keeps crashing on the last timeunit when trying to write to file and I can't figure out why. It manages to write to the file on each other timestep (no matter how many I put in), with the correct file name e.g. "Map_time_0.txt", but not the last.

I thought this might be an indexing problem, but I can't see any reason why changing the time value will cause this to crash.

Gives this error:
Unhandled exception at 0x7c812afb in Puccinia striiformis.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012fb04..

This is a summary of the writing parts of my code:

// I'm not entirely sure all these headers are relevant here, but thought
// I'd better include them just incase.
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>

int main(void)
        int TIMESTEPS=2;
	int Time=0;
		// Each timeunit...
			// Update the proportion infected per bin
			// Write coordinates and severity to a file.
	        // Check for infection, and then change states of relevant plants
	        // Check whether to EXIT or not




And the functions:

void Check_exit(){
	if(Time>=TIMESTEPS*TIMEUNITS) Exit_flag=1;
	else Exit_flag=0;

void Write_maps(int Time){
	std::string file_name, cTime;
	std::cout << Time << std::endl;
	std::stringstream ssFileName;
	ssFileName << "Map_time_";
	ssFileName << (int)Time/TIMESTEPS;
	ssFileName << ".txt";
	std::cout << ssFileName.str() << std::endl;
	file_name = ssFileName.str();
	std::cout << "File name = " << file_name <<std::endl;
	std::ofstream myfile( file_name.c_str() );
// Print out results
	for(int i=0;i<NO_OF_PLANTS;i++){
		myfile << plants[i].X_coord << "," << plants[i].Y_coord << "," << plants[i].severity() << std::endl;

The program manages to print out the File name = ..., and then seems to crash when trying to create the myfile ofstream object.

Any help would be appreciated.

Recommended Answers

All 3 Replies

Where's the definition of Update_records() and TimeStep()? and where did you declare NO_OF_PLANTS and plants[]? Please post the full code.

At line 11 of your functions code, in Write_maps(), your cast-to-integer doesn't do anything. The expression is equivalent to (int(Time))/TIMESTEPS , and Time is already an integer.

TIMESTEPS is presumably also an integer, but you apparently have it defined globally (if you can access it in Write_maps()) and also locally in your main(). If you already have it globally available, you don't need it in main(), and it just invites problems since if you want to change it, you need to change it in more than one place.

And if both values are integers, then the division results in an integer, so no cast is needed anyway.

How does your Check_exit() function know the value of Time (also local to main())?

And what is the last filename printed out in your "Filename = ..." line?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.