Hello out there,

I am trying to compile some simple code but am running into a weird error that I can not figure out

terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear

This is happening when I do an Infile>> read in the code below. What I have done is commented out the read piece by piece to see what variable is causing the issue, and it happens when I start reading in the float variables d1-d10. Any clues?

Thanks in advance

Devang N. Joshi
Homework One
CSCI 325 - Averaging in C++
January 19th, 2011

The purpose of this program is to serve
as basic review on simple file I/O in
C++ using a well structured data file
and performing simple math on it.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
	//declare all variables
	ifstream Infile;
	ofstream Outfile;
	int stationNum = 0; //variable for the station number 
	char dateCode[13];  //variable for the date code
	char dataType[3];   /*variable that shows what "type" the data is
			    example...pwl stands for "primary water level"*/

	float d1=0;
        float d2=0; 
	float d3=0;
	float d4=0;
	float d5=0;
	float d6=0;
	float d7=0;
	float d8=0;
	float d9=0;
	float d10=0;
			    //ten variables for water levels over an hour

	double daySum=0;    //sum of all the water levels over 24hrs
	double dayAvg=0;    //average water level for a given day

	double denominator=240;
			    //240 data points per day, used to cal dayAvg

	int rowCount=0;     //counter used to keep track of day change

	//open and test files for input & output
		cerr<<"Error with input file....terminating program";
		cerr<<"Error with output file....terminating program";

	//initial write to output file...write headings to output file
	Outfile<<"Date"<<"	"<<"Average Water Height"<<endl;

	Logic to calculate average daily water levels and
	print them to the output files                   

	/*while(!Infile.eof()) //test against end of file marker
		if(rowCount<24) //at 24 one day has elapsed

		/*else //picked up when one days worth of data is read in, can be averaged now
			dayAvg=daySum/denominator; //calculate the average
			Outfile<<dateCode<<"	"<<dayAvg<<endl; //print data to the output file
			daySum=0;    //reset daysum
			dayAvg=0;    //reset dayavg
			rowCount=0;  //reset rowcount

	//final write to output file to mark end of data
	Outfile<<"End of data"<<endl;

	//close input & output files

	return 0;
6 Years
Discussion Span
Last Post by Fbody
Featured Replies
  • 1

    I noticed that your char arrays (C-Style strings) are not initialized. I would consider fully initializing them with NULLs.[CODE] char myCharArray[15] = {'\0'};[/CODE] I also noticed that the "dataType" array is only 3 char long, you may want to consider making it at least 4 so that you have room … Read More


What does one line of the data file look like?

Somewhere you're trying to read something that doesn't match the type you have specified for it.

Just as an example, I know this is probably not the situation you're experiencing:

1 pw 30.0 2.0

int i;
int j;
double x,y;

Infile >> i >> j >> x >> y; //doesn't know what to do with the pw, stops reading

Also, why not use an array for d1 through d10?

Edited by jonsca: n/a


Oh so sorry about not including the data, here is the first line of the file:

008 2011006+0000 pwl 1.423 1.424 1.429 1.432 1.436 1.439 1.443 1.446 1.447 1.452


Can you post up the entire data file? I want to see if I can reproduce the error? I was able to read in the row you provided with no problem.


Does the code read any of the data from the file? As Jonsca said, that one line is OK. If it's just one line in a file, you can use a try... catch statement to catch the exception and print out the line number of the line that has a problem, to aid debugging. So something like:

if(rowCount < 24) //at 24 one day has elapsed
      Infile >> stationNum >> dateCode >> dataType >> d1 >> d2
             >> d3 >> d4 >> d5 >> d6 >> d7 >> d8 >> d9 >> d10;
   catch(std::runtime_error &e){
      std::cerr << "Error on row " << rowCount + 1 << std::endl;
      return 1;

You need to include stdexcept for the definition of std::runtime_error .


I noticed that your char arrays (C-Style strings) are not initialized. I would consider fully initializing them with NULLs.

char myCharArray[15] = {'\0'};

I also noticed that the "dataType" array is only 3 char long, you may want to consider making it at least 4 so that you have room for a NULL after the read data. Do you know how long the longest dataType string is going to be? Your array will have to be that length plus (1) to allow for the data plus the NULL.

Perhaps your issues have something to do with this, but are compiler-specific?

Using the provided data, when I try to run your code in Visual Studio I get this:

Run-Time Check Failure #2 - Stack around the variable 'dataType' was corrupted.

I strongly suggest that you enlarge "dataType". When I changed it to 4 elements, it ran fine.

I suspect that jonsca's and ravenous' compilers are organizing the memory differently and thus circumventing the issue.

Edited by Fbody: n/a


I strongly suggest that you enlarge "dataType".

I think you're right on about that. I had thought about it, but I wasn't sure if the extraction operator put a null at the end of C-Strings. Evidently it does (which makes sense):

From: http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

Extracts characters and stores them as a c-string (i.e. in succesive locations starting at location pointed by str and terminated by a null-character). Extraction ends when the next character is either a valid whitespace or a null character, or if the End-Of-File is reached.
The terminating null character is automatically appended after the extracted characters.


Thanks for all the help, reading Fbodys comment about the error given by Visual Studio (that the data around data type was corrupt) I played around with the size of the char dataType. I changed the char size from 3 to 4 and it works now. I think perhaps there just was not enough memory? at any rate thanks for the help!


>>I think perhaps there just was not enough memory?

When you overrun the boundaries of an array (which dataType is), you corrupt adjacent memory. How the system reacts to this corruption depends on the contents of the memory corrupted. The system may completely ignore it or it may crash. It just depends. Either way, it's a serious error that should be avoided.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.