int main (int argc, const char * argv[])
{
int numberOfAtoms = 0;
ifstream in;
char atomsIn[12];
in.open("Volume.crd");
if (!in.is_open())
cerr << "File not open\n";
if (in.is_open()) {
in.getline(atomsIn, 15, '\n');
numberOfAtoms = atoi(atomsIn);
}
string tempIn;
cout << "getline" << endl;
getline(in, tempIn);
return 0;
}
When I compile there are no errors but when I run this I get the following error:
getline
Segmentation fault: 11
I can eliminate the error by commenting out getline(in, tempIn);
Any help would be greatly appreciated.
The prototype for the getline function you are using is:
istream& getline (char* s, streamsize n, char delim );
This will read a maximum of n - 1 characters, including the delimiting character (in your case '\n') and store at most n - 1 characters and afix a null character to terminate the C style string. The delimiting character is read but not stored. If there are more than n - 1 characters read the excess characters (those beyond the first n - 1 characters are discarded and the fail bit on the stream is set. The line in.getline(atomsIn, 15, '\n'); can potentially store 14 characters plus the terminating null character, but atomsIn was declared to hold only 12 characters so the call to getline is probably running past the end of the array. Try changing the line:
char atomsIn[12]; to char atomsIn[15];
This should enable you to read in up to 14 characters and even if a longer string is input the program should not crash though the failbit on the stream may be set.
http://www.cplusplus.com/reference/string/getline/
istream& getline ( istream& is, string& str );
This is the function that I was using. I think I understand what you are saying but would it apply in this case? I was confused because I thought that a string would automatically resize itself based on the size of input.
You were using a char array, not a std::string. If you want to use the prototype for getline that you posted, change char atomsIn[12] to string atomsIn;. Be sure to #include <string> of course.
I am sorry for not being a little more specific in my original post. The first getline() that is storing info in atomsIn is working properly. The getline() that is not functioning is the one right before the return statement. When I comment that line out the program executes perfectly. When that line executes I get seg fault 11. Sorry for the confusion, I should have written that first post a little clearer.
Chris
Undefined behaviour is undefined. getline(in, templn); is not working because you corrupted the memory with in.getline(atomsIn, 15, '\n'); (writing out of bounds)
¿Is there a good reason for you to use char* instead of std::string?
I did it because I needed to use atof() and I didn't know how to use it with a string. My other problem is that I am taking a line of code that contains 4 floating point numbers.
The following code shows how I am getting the input. It is a terrible set way to do things, but it works for now.
class atom{
public:
double x, y, z, radius;
};
int numberOfAtoms = 0;
double boxVolume = 0, xmin, xmax, ymin, ymax, zmin, zmax;//initially hold the min and max from input. ends up holding min max for the box
int throws = 1000000, sum = 0;
double ux, lx, uy, ly, uz, lz;//this will determine the u bound & l bound
int main()
{
ifstream chris;
chris.open("Volume.crd");
string temp;
int waste = 0;//holds a meaningless value
char tempx[13], tempy[13], tempz[13], tempr[13];
chris.getline(tempx, 13);
numberOfAtoms = atoi(tempx);
atom holder[numberOfAtoms+1];
int count = 0;
while(!chris.eof() && (count < numberOfAtoms))
{
getline(chris, temp);
waste = temp.copy(tempx, 12, 0);
waste = temp.copy(tempy, 12, 12);
waste = temp.copy(tempz, 12, 24);
waste = temp.copy(tempr, 12, 35);
holder[count].x = atof(tempx);
holder[count].y = atof(tempy);
holder[count].z = atof(tempz);
holder[count].radius = atof(tempr);
count++;
}
The program is overly complicated because of the way I set it up (int waste is a great example of this). I am learning to program and unfortunately this was the best I could do with the limited time that I have. The program isn't due for another 4 weeks but the next four weeks are busy for me so I am trying my best to get it done this weekend. I am far from proficient with C++ and the built in functions so I just use the ones that I know. I included most of the code for the program. Any suggestions would be greatly appreciated