Hello

I am trying to read from a file. I'm followed a tutorial and had it working perfectly before, and it still does now..
But the problem is that one variable that I'm reading from the file is causing my program to crash.

All the other vars I'm reading, are fine. And I know its the one variable cause I pinpointed it out. Commented it out, the program runs fine. With it, crash.

The crash is a simple "The program has stop responding..." then closes.

The other problem is that this value is important to read, as without it there's no point in making the program :D

Ok the variable, is a simple integer, something like 208. (Remember that)
But all the values from the file are ints too.
And some are much larger then the one above, and load fine. As well as display fine when displayed.

The other strange problem with this is if its a value of 8< its fine. Displays, works etc..
But..I need values < 100...and why anything after 8 causes it to crash is beyond me.oo

Also adding more values to read results in nothing. Still reads.
Moving where it reads this int still causes crash.

Renaming the variable it stores to still a crash.

So now I am just out of ideas on what to do, cause its not only looks right, doesn't trigger any of the errors or custom exits. And is exactly the same way as the old program and tutorial, just with more values being read.

Oh and another strange thing about this, and how I knew it was 8< cause I had a string at the beginning of the file.
And I did getline etc.. and after that came the value 208.
It worked....except that it cut the 200 and left it with 8........................

Its like the program doesn't like the 208 or just plain that variable...

Heres an example:

bool Load(int Level) {
    if (Level == 0 || Level >= 21) { return false; }
    char buffer[200];
    sprintf(buffer, "Resources/%d.ini", Level);
    ifstream load;
    load.open(buffer);
    if (load != NULL) {
        load >> M;
        load.ignore();
        load >> T;
        load.ignore();
        load >> W;
        load.ignore();
        load >> H;
        load.ignore();
        load >> Start;
        load.ignore();
        load >> Startx;
        load.ignore();
        //load >> E;       //This is whats crashing it....unless its 8
        //load.ignore();
        
        load.close();
    } else { return false; }
    return true;
}

And example ini file:

1
200
1280
1000
100
800
208
0

Sorry for such a long post.
Such a simple problem completely stops this program and causing me to pull my hair out!
And like I said it worked, works, and should cause its just that one variable thats the same as the others that doesn't like me for some reason.

[searching=]http://www.daniweb.com/forums/thread68731.html[/searching]
Somewhat similar to mine except I'm dealing with variables.

Thanks

-Bill

Where are M,T,W,H, etc. declared, and what are their types?

Their all the same and declared at the top of the script.

//Top of script

int M       = 0;
int T       = 0;
int W       = 0;
int H       = 0;
int L       = 0;
int Start   = 0;
int Startx  = 0;
int E       = 208; //Have to manually put it to 208

Since its all int variables I'm working with.

Try the following program. Note the warning about global variables.

#include <iostream>
#include <fstream>

int M = 0,T=0,W=0,H=0,L=0,Start=0,Startx=0,E=0;
//Global variables should be avoided! but I'm trying to emulate your program

int main()
{
     std::ifstream load("ifstreamcrash.txt");
	
     if (load != NULL) {  //use .is_open() method instead
        load >> M;
	std::cout<<"M= "<<M<<std::endl;
		
        load >> T;
	std::cout<<"T= "<<T<<std::endl;
        
        load >> W;
        
        load >> H;
        
        load >> Start;
        
        load >> Startx;
        
        load >> E;       
	std::cout<<"E = "<<E<<std::endl;
		
	} //I didn't close the stream because the program ends here, otherwise you're right
}

Nope. Nothing.

Now that I look over something, could it be because I'm setting the value of E, then RIGHT after I use the value?

Ex.

void Load() {
//Above
}

int main() {
    Load();
    E = 1; //etc..
}

Still don't understand why anything higher then 8 crashes it though.

Edit:
Ran it in Debug mode and I got an error segmentation fault
Checked it out, program points to a line where the E value is used in a loop. If this value is anything less or more then whats in another file Im reading, theres problems.
So What I said above could be it?

But still why it accepts 8 and lower but nothing else :/

Nope. Nothing.

Now that I look over something, could it be because I'm setting the value of E, then RIGHT after I use the value?


Edit:
Ran it in Debug mode and I got an error segmentation fault
Checked it out, program points to a line where the E value is used in a loop. If this value is anything less or more then whats in another file Im reading, theres problems.
So What I said above could be it?

But still why it accepts 8 and lower but nothing else :/

I was trying to show that the file read works fine in isolation, so it is definitely somewhere else in your program. Since I cannot see the rest of your program it is difficult to gauge. Test your file reading routine on the other file and see if it's general enough.

Ok so I did a bit more on finding out this issue. And yes I meant the reading was ok.

Ok its not really the variable causing problems, but When its used its causing problems.

Ok so all my variables are being read fine, and being assigned right. Just like it should.
I created a new variable to simply hold the E value and simply made E 208 so I could run the program.
It works. Then I printed the result of the new variable that was read from the file, and yet displays what it should.

Edit...
I have to assign a default value to variables so that they can be used?

When I set the E value to anything higher then the number in the file, it works and doesn't crash..also displays 208 in the debug text in the program...

Wheres the rest of my variables, it doesn't matter. 0 or not it works.

Well at least this is solved but I still want to know why this has to be or it will crash, and its a simple variable.

@jonsca appreciate the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.