1. Post #1
    Gold Member
    raccoon12's Avatar
    November 2008
    3,175 Posts
    sorry I ask dumb questions...
    I tried debugging, it sends me to the bracket under int main() and crtexe.h

    I get this.. er.. thing when I run it:
    Unhandled exception at 0x76e9b727 in stuff.exe: Microsoft C++ exception: std::out_of_range at memory location 0x003ef690..

    here's the code:
    	void ParseFile(char* pFil)
    	{
    		std::fstream fil(pFil, std::fstream::in | std::fstream::out | std::fstream::trunc);
    		while(!fil.eof())
    		{
    			std::string sParse;
    			std::string line;
    			std::getline(fil, line);
    			sParse = line.substr(12, 64);
    			std::cout << sParse;
    		}
    	}
    
    Reply With Quote Edit / Delete Reply United States Show Events Dumb Dumb x 1 (list)

  2. Post #2
    Dennab
    December 2006
    1,979 Posts
    Misallocated memory: you win!

  3. Post #3
    Gold Member
    raccoon12's Avatar
    November 2008
    3,175 Posts
    teach me

  4. Post #4
    Dennab
    December 2006
    1,979 Posts
    I don't do much C++ so I can't point out the error by looking at your code. In your case, neither can the debugger. Something went wrong. It can be your memory, the way you wrote the code, the way the debugger is written, WINDOWS or other crap malfunctioning. Bottom line SOMETHING tried reading SOMETHING ELSE'S allocated memory. No you didn't really win.
    Reply With Quote Edit / Delete Reply Canada Show Events Dumb Dumb x 3Agree Agree x 1Optimistic Optimistic x 1 (list)

  5. Post #5
    Gold Member
    efeX's Avatar
    April 2009
    2,332 Posts
    I don't do much C++ so I can't point out the error by looking at your code. In your case, neither can the debugger. Something went wrong. It can be your memory, the way you wrote the code, the way the debugger is written, WINDOWS or other crap malfunctioning. Bottom line SOMETHING tried reading SOMETHING ELSE'S allocated memory. No you didn't really win.
    the fuck are you on about buddy.
    Reply With Quote Edit / Delete Reply United States Show Events Agree Agree x 9 (list)

  6. Post #6
    Dennab
    December 2006
    1,979 Posts
    Sorry I watched a soap opera.
    Reply With Quote Edit / Delete Reply Canada Show Events Funny Funny x 10 (list)

  7. Post #7
    Wyzard's Avatar
    June 2008
    1,243 Posts
    Actually a std::out_of_range exception occurs when you give a nonexistent index to a container that does bounds checking, such as a std::string.

    The problem is probably on line 9, where you're trying to get characters 12 through 64 of your string -- if the string is less than 12 characters long, you'll get that exception. (The second parameter is allowed to be past the end of the string, but the first parameter is not since you can't begin at a place that's past the end.)

  8. Post #8
    Gold Member
    jA_cOp's Avatar
    May 2006
    2,683 Posts
    It's probably line.substr(12, 64); that is causing it.

    Check if the string is long enough before doing that.

    Finally, you should learn more about the debugger. You can set a breakpoint right before calling ParseFile and step line-by-line through the code and see exactly what threw the exception.

  9. Post #9
    Gold Member
    raccoon12's Avatar
    November 2008
    3,175 Posts
    if I do line.substr(12, line.length()); it throws the same thing

    also, where can I find something to teach me about the debugger?
    Reply With Quote Edit / Delete Reply United States Show Events Optimistic Optimistic x 1Funny Funny x 1 (list)

  10. Post #10
    Gold Member
    jA_cOp's Avatar
    May 2006
    2,683 Posts
    if I do line.substr(12, line.length()); it throws the same thing
    That's because, as said, the line doesn't have as much as 12 characters in it. The second argument is irrelevant.

    also, where can I find something to teach me about the debugger?
    Just google it...

  11. Post #11
    Gold Member
    jivemasta's Avatar
    January 2005
    315 Posts
    You need to do something like:

    Code:
    if(line.length >= 12)
        sParse = line.substr(12,64);
    To check that line is long enough first. I don't know what you are trying to do, but you should be able to apply that to your goal.

  12. Post #12
    Gold Member
    raccoon12's Avatar
    November 2008
    3,175 Posts
    main.cpp
    #include "File.h"
    //#include "CPlayer.h"
    #include <iostream>
    #include <windows.h>
    
    int main()
    {
    	SetConsoleTitle("Opening file..."); 
    	File files;
    	std::cout << "Writing to file...\n";
    	files.ParseFile("Raccoon.txt");
    	std::cin.get();
    	return 0;
    }
    

    updated Files.h
    #include <fstream>
    #include <iostream>
    #include <string>
    
    std::string inTypes [2] = { "playername =", "playerhealth =" };
    class File
    {
    public:
    	void WriteFile(char* pFile, std::string pMessage)
    	{
    		std::fstream file(pFile, std::fstream::in | std::fstream::out | std::fstream::trunc);
    		file << pMessage;
    		if(file.fail())
    		{
    			std::cout << "Failed to open file...";
    		}
    		file.close();
    	}
    	
    	void ParseFile(char* pFil)
    	{
    		std::fstream fil(pFil, std::fstream::in | std::fstream::out | std::fstream::trunc);
    		while(!fil.eof())
    		{
    			std::string sParse;
    			std::string line;
    			std::getline(fil, line);
    			if(line.length() <= 2)
    				sParse = line.substr(2, 2);
    			std::cout << sParse;
    		}
    	}
    };
    

    Gives me the same error

  13. Post #13
    I survived Camp FP 2010
    Mattz333's Avatar
    June 2007
    1,656 Posts
    Where is the file located? With different IDEs and compilers, the working directory can be different to your code directory. Try Putting your file in C:\ and making the file location string: C:\Raccoon.txt

    Edit: That's wrong, sorry.
    Reply With Quote Edit / Delete Reply United States Show Events Dumb Dumb x 1 (list)

  14. Post #14
    Gold Member
    raccoon12's Avatar
    November 2008
    3,175 Posts
    ja_cop was right, it is line.substr, but I don't know why
    the file has "playername = tim" in it