I have written a small program designed to read file input and put this information into a multidimensional array. As it stands, the program reads just one file for now, the name of which I specify when calling readfile.open. However, I have now altered the program so that the readfile function accepts a c-style string (I need to do this so that in the future I can alter the name of the file by looping over variables). The program now crashes with the following message:
terminate called after throwing an instance of 'std::bad_alloc'
The program compiles fine; I am using g++ on red hat. Here is the program source code:
readfile.open(filename.c_str(), ios::in | ios::binary | ios::ate);
size = readfile.tellg(); // Determine size of input file using the get pointer //
readfile.seekg(0, ios::beg); // Put pointer back to beginning of file //
zbuff = new complex<double>[size]; // < -- Execution stops here. zbuff is simple a one dimensional complex array //
readfile.read(reinterpret_cast <char *> (zbuff), size);
for(k = 0; k < spaceh; k++)
for(j = 0; j < Nc; j++)
for(i = 0; i < 4; i++)
Psi[i][j][k][srcdir] = convert_complex(zbuff[kount]); // Take info in 1-d array zbuff, change endianness of entry, place in correct element of 4-d array Psi //
As I said, the program works perfectly when the filename is given explicitly. Only when I pass the filename as a string does the program crash. Curiously, the crash does not occur at the filename line, but later on, when "new" is called. The bas alloc suggests I have run out of space for something. I don't see how that is possible, since nothing is particularly large here. zbuff is the largest creation here, but there were no complaints before I made this minor change, and I also intent to make zbuff considerably larger in the future!
The file definitely exists in the directory, and "size" itself was determined by putting the get pointer at the end of the file. The file is roughly 1.3 Mb in size. In any case, this method worked very well before I altered the way of telling the program the filename. Apparently, for this flexibility with filenames, you need to provide readfile.open with a c-style string.
ifstream:: pos_type is no basic integer type, but it seems it´s implicitely convertible to an integer type.
As Richard already pointed out something seems to go wrong when opening the file because the m_off member is set to -1, which indicates an invalid file position.
Your suggestion is actually the original form of my program - providing the string explicitly. I can certainly change the name of the file each time I edit the source code, but my goal is to be able to loop through certain variables (mainly integers) which form part of the file name itself. I wish to read multiple files using such a loop, and that data is to be read into the a (larger) multidimensional array.
I've assumed here that I need to use the stringstream class if I want to include variables in the file name. The different numbers in the file name I've used actually correspond to different integers I need to loop over.
Sorry about the missing code tags, it seems I can't edit posts according to the permissions I have.