Raised This Month: $51 Target: $400
 12% 

Invalid file handle 0


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bladesmc
Junior Member
Join Date: May 2016
Old 02-07-2018 , 19:48   Invalid file handle 0
Reply With Quote #1

So I'm trying to open a file in sourcepawn, it's just a txt file, and I'm going to store some IDs and values next to them, but for some reason the file returns an error when I try to open it. I think something is wrong with when I try to define the handle? Everything compiles fine, no warnings or errors, but when I try to actually call the code it suicides...

The troublesome code:
PHP Code:
public bool StoreElo(int clientint value) {
    
int ClientSteamID GetSteamAccountID(clienttrue);
    
char id[64];
    
char line[64];
    
char data[64];
    
char elo[64];
    
Handle datafile;
    
    if(
ClientSteamID == 0) {
        return 
false;
    }
    
    
IntToString(ClientSteamIDidsizeof(id));
    
    
BuildPath(Path_SMfilepathsizeof(filepath), "data\elo.txt");
    
datafile OpenFile(filepath"r+");
    while(!
IsEndOfFile(datafile) && ReadFileLine(datafilelinesizeof(line))) {
        if(
StrContains(lineidfalse)) {
            
//Do stuff
            
            
CloseHandle(datafile);
            return 
true;
        }
    }

The error msg:
Code:
L 02/07/2018 - 16:38:09: [SM] Exception reported: invalid handle 0 (error: 4)
L 02/07/2018 - 16:38:09: [SM] Blaming: basic-elo.smx
L 02/07/2018 - 16:38:09: [SM] Call stack trace:
L 02/07/2018 - 16:38:09: [SM]   [0] IsEndOfFile
__________________
My plugins:
Bladesmc is offline
xerox8521
Senior Member
Join Date: Sep 2011
Old 02-07-2018 , 20:16   Re: Invalid file handle 0
Reply With Quote #2

Are you running on Windows ? then you might need to escape \ aka data\\elo.txt. Not sure if /data/elo.txt works on windows.
Also you should always verify that the handle you receive is not null or INVALID_HANDLE.
xerox8521 is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 02-08-2018 , 08:37   Re: Invalid file handle 0
Reply With Quote #3

Your code is closing the handle while reading each line of the file. Hence why it throws that error.

I suggest moving "CloseHandle(datafile);" at the bottom and add a bool to help you confirm if the "StrContains" part returns either -1 or something else.

Last edited by cravenge; 02-08-2018 at 08:38.
cravenge is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 02-08-2018 , 09:30   Re: Invalid file handle 0
Reply With Quote #4

Quote:
Originally Posted by cravenge View Post
Your code is closing the handle while reading each line of the file. Hence why it throws that error.

I suggest moving "CloseHandle(datafile);" at the bottom and add a bool to help you confirm if the "StrContains" part returns either -1 or something else.
Doubt that's why, he returns out of the function as soon as the handle is closed.
My guess would be that the file/path you're trying to open isn't valid.

I'm not sure if this could cause it but you should try escaping the escape character:
"data\elo.txt" -> "data\\elo.txt"

Basically what xerox said

Last edited by hmmmmm; 02-08-2018 at 09:31.
hmmmmm is offline
cravenge
Veteran Member
Join Date: Nov 2015
Location: Chocolate Factory
Old 02-09-2018 , 23:02   Re: Invalid file handle 0
Reply With Quote #5

Use backslashes, not forward ones.

Last edited by cravenge; 02-09-2018 at 23:05. Reason: Actually, there was a problem with the BuildPath part...
cravenge is offline
CrazyHackGUT
AlliedModders Donor
Join Date: Feb 2016
Location: Izhevsk, Russia
Old 02-10-2018 , 02:04   Re: Invalid file handle 0
Reply With Quote #6

This code should work properly on Windows, Linux and MacOS.
PHP Code:
public bool StoreElo(int clientint value) {
    
int ClientSteamID GetSteamAccountID(clienttrue);
    
char id[64];
    
char line[64];
    
char data[64];
    
char elo[64];
    
Handle datafile;
    
    if(
ClientSteamID == 0) {
        return 
false;
    }
    
    
IntToString(ClientSteamIDidsizeof(id));
    
    
BuildPath(Path_SMfilepathsizeof(filepath), "data/elo.txt");
    
datafile OpenFile(filepath"r+");
    if (!
datafile) {
        return 
false;
    }

    while(!
IsEndOfFile(datafile) && ReadFileLine(datafilelinesizeof(line))) {
        if(
StrContains(lineidfalse)) {
            
//Do stuff
            
            
CloseHandle(datafile);
            return 
true;
        }
    }

    
CloseHandle(datafile);
    return 
false;

__________________
My english is very bad. I am live in Russia. Learning english language - very hard task for me...
CrazyHackGUT is offline
Send a message via ICQ to CrazyHackGUT Send a message via Skype™ to CrazyHackGUT
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode