reading file weird access violation

This is a discussion on reading file weird access violation within the C++ Programming forums, part of the General Programming Boards category; the following is a part of my sprite loading function, using vc++ .net 7 running in the debuger and running ...

  1. #1
    p3p
    p3p is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Exclamation reading file weird access violation

    the following is a part of my sprite loading function,
    using vc++ .net 7

    running in the debuger and running the debug exe it runs perfectly, running the release exe from the debuger runs perfectly but on running the release exe outside the debuger it crashes sumtimes not all the time after the log "Loaded sprite" and before the Archive is asigned,

    all it could be is the fscanf or fgets i tried both thats y it looks messed up.

    any help would be apreitiated.

    p3p

    Code:
    	char tempfiledat[256],buffer[256];
    	std::string filename,ofilename,Archive;
    	int pause=0, r=0, g=0, b=0, w=0, h=0, x=0, y=0, Bx=0, By=0;
    	FILE *fp;
    
    	if(iniFile[0]=='.'){
    		filename=parse("%s.ini", iniFile.c_str());
    	}else{
    		filename=parse("%s%s.ini", SPRITEDIR,iniFile.c_str());
    	}
    	
    	Log(T_LOG,"Loading Sprite: \"%s\"", filename.c_str());
    
    	if((fp=fopen(filename.c_str(), "r+")) == NULL){
    		Log(T_ERROR,"opening file %s", filename.c_str());
    		return -1;
    	}
    	//Log to see if crash is in file load
    	Log(T_LOG,"Loaded Sprite: \"%s\"",filename.c_str());
    	
    	fscanf(fp, "BIN: %s", tempfiledat);
    	fgets(buffer, 255, fp);
    	Archive=parse(tempfiledat);
    
    	fscanf(fp, "FILE: %s", tempfiledat);
    	fgets(buffer, 255, fp);
    	filename=parse(tempfiledat);
    	
    	fscanf(fp, "OVERLAY: %s", tempfiledat);
    	fgets(buffer, 255, fp);
    	ofilename=parse(tempfiledat);

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    mixing c and c++, you should pick one or the other and stick with
    it.

    while you go over that i will be converting your code to c++.

    which i will post in a few.

  3. #3
    Banned
    Join Date
    Jun 2005
    Posts
    594
    unfourtantly i dont remember a whole lot from c, because i choose way back when i was beginning to stick with c++

    Code:
    string iniFile, tempfiledat, buffer, filename, ofilename, Archive;
    	int pause = 0, r = 0, g = 0, b = 0, w = 0, h = 0, x = 0, y = 0, Bx = 0, By = 0;
    	
    
    	if(iniFile.find(".ini", 0) == string::npos)
    	{
    		iniFile += ".ini";
    	}
    	
    	//not sure what your intended purpose is with this
    	//so i made no changes
    	Log(T_LOG,"Loading Sprite: \"%s\"", filename.c_str());
    
    	ifstream in(filename.c_str() );
    	if(!in.is_open())
    	{
    		Log(T_ERROR,"opening file %s", filename.c_str());
    		return -1;
    	}
    	//not sure what your intended purpose is with this
    	//so i made no changes
    	Log(T_LOG,"Loaded Sprite: \"%s\"",filename.c_str());
    	
    	Archive = "BIN: ";
    	getline(in , tempfiledat , '\n');
    	Archive += tempfiledat;
    
    	filename = "FILE: ";
    	getline(in , tempfiledat , '\n');
    	filename += tempfiledat;
    	
    	ofilename = "OVERLAY: ";
    	getline(in , tempfiledat , '\n');
    	ofilename += tempfiledat;
    wha ti could make of it heads or tails, but without your full code you may not beable to plug this in, and i cant really test it
    to see if it fixes your error. and you didnt really specify
    your error, so with out the entire code i cant even begin to guess.
    mostly though because im nto fimilar with C as much.


    to use the change i made in there
    you will need to use

    <fstream> and <string>
    headers and i included using namespace
    because my personal prefrence, i dont like typing
    std:: in front of everything.
    Last edited by ILoveVectors; 09-02-2005 at 05:50 PM.

  4. #4
    p3p
    p3p is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    thank you,
    i will convert to ifstream, the function is more then 200 lines long and is dependant on a fair amount of other custom functions and classes so posting compilable code wouldnt be posible,

    the error is inexplicable as i tried to explain (ive noidea wats goin on),

    (the fscanf strips the "BIN: " ect. from the string loaded from the sprite ini file and puts the rest in tempfiledat for future reference)

    (
    Code:
    	if(iniFile[0]=='.'){
    		filename=parse("%s.ini", iniFile.c_str());
    	}else{
    		filename=parse("%s%s.ini", SPRITEDIR,iniFile.c_str());
    	}
    checks to see wether its passed a relative directory or just the filename)

    chz
    p3p
    Last edited by p3p; 09-02-2005 at 06:06 PM.

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    in the code i posted i dotn think you will expereince that problem.

  6. #6
    p3p
    p3p is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    ok traced it to the file not staying open sumtimes, made the program check and try reopening it 10 times with a 500ms delay between atemps n it still fails, will get back 2 it later need sleep

  7. #7
    p3p
    p3p is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    well i wrote a inifile handling class and still had the same error until i added

    Code:
    bool PINIFile::active()
    {
    	int i=0;
    	if(inifile.is_open() && inifile.good()){ return true; }
    
    	while(!inifile.good() && i < 10){
    		reinit();
    		i++;
    	}
    	if(!inifile.good()){close(); return false; }
    	return true;
    }
    
    bool PINIFile::reinit()
    {
    	if(wasinit)
    	{
    		close();
    		SDL_Delay(500);
    		Log(T_ERROR,"File %s Went Bad Trying to reopen",inifilename.c_str());
    		inifile.clear();
    		inifile.open((inifilename.c_str()));
    	}
    	else 
    		return false;
    	return true;
    }
    now it works fine apart from the ocasional error in the log file and delayed file opening, wonder y its loses the open stream, ..

    p3p

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. renaming executable file causes access violation
    By m37h0d in forum C++ Programming
    Replies: 8
    Last Post: 05-29-2008, 09:41 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21