Strange File I problem.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Strange File I problem.

  1. #1
    Join Date
    Sep 2009
    Posts
    2

    Strange File I problem.

    Hi,

    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;
    	//}
    }
    ... then the program runs fine.


    Any and all help is greatly appreciated!

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: Strange File I problem.

    I think your problem is here:
    Quote Originally Posted by Matt000r000 View Post
    Code:
    			for (int i=0; !isCalendar(str)&&!file.eof(); i++) {
    				file>>str;
    				cout<<str<<' ';
    			}
    Have a look at this FAQ.
    Quote Originally Posted by Matt000r000 View Post
    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

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: Strange File I problem.

    Also, I think you are making the serach more difficult than it needs to be:

    Code:
      std::string word;
      bool bFound = false;
      
      while ( !bFound && (file>>word) ) 
      {
        if (word == "Calendar")
          bFound = true;
      }
      
      if (bFound)
      {
        cout << "found .. rest of line = ";
        getline(file,word);
        cout << word << "\n";
      }

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Strange File I problem.

    Did you step through it with the debugger?

    The debugger wil tell you exactly what line it's broken on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center