I've been trying to debug this code for 4 days now. I can't figure out why it crashes. Here's my source code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
bool isCalendar(char * str);
int main (int argc, char * const argv[]) {
//Load the file in question.
{
ifstream file;
file.open("temp.txt", ios::binary);
if (file.is_open())
{
char str[100];
for (int i=0; i!=100; i++) {
str[i]='E';
}
for (int i=0; !isCalendar(str)&&!file.eof(); i++) {
file>>str;
cout<<str<<' ';
}
cout << endl;
for (int i=0; i!=90; i++) {
cout << str[i] << '-' << (int)str[i] << endl;
}
} else {
cout << "Unable to open file";
}
file.close();
}
}
bool isCalendar(char * str)
{
//return true;
if (str[0]=='C') {
if (str[1]=='a') {
if (str[7]=='r') {
if ((int)str[8]==0) {
return true;
}
}
}
}
return false;
}
The text file is this:
Code:
Blah, blah, blah
CalendarNotRightOne
Calendar" -not right one either
Calendar -right one.
blah, blah, blah.
It's not the actual file, but it has all the same elements.
The program searches through a long text file for the one and only phrase "Calendar" without anything else around it. There are multiple other instances of the same word, but they either have other words conjoined to them or have various punctuation attached. All three of these scenarios are represented above. For some reason, after preforming its duty perfectly, when it reaches the end of the file, it crashes. The only info I'm given is: "Program received signal SIGABRT" and the breakpoint window goes to a thread named "__kill", which is inside a thread named "__abort", which is inside a thread named "__stack_chk_fail" which is inside my main thread. Can anyone help me decipher these mysterious warnings, and also help me to fix my code?
I have also found, in my old-fashioned way of commenting out code and experimenting, that if you comment out the isCalendar() function like so:
Code:
if (str[7]=='r') {
//if ((int)str[8]==0) {
return true;
//}
}
I have also found, in my old-fashioned way of commenting out code and experimenting, that if you comment out the isCalendar() function like so:
Code:
if (str[7]=='r') {
//if ((int)str[8]==0) {
return true;
//}
}
... then the program runs fine.
That's probably because the function now returns true where it previously returned false. Hence, your loop is ended before you reach the end of the file.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Bookmarks