Raised This Month: $ Target: $400
 0% 

read_file ( const file[], line, text[], len, &txtLen )


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-09-2011 , 11:07   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #1

Quote:
Originally Posted by gamer99 View Post
Thanks a lot ... i will try it

One more thing if the file does not exits then how to wirte that file and make the first entry ?

for example

Code:
if(!file_exists(file))
{
file_handle = fopen(file, "wt")
//holder is the string
fputs(file_handle, holder)
	fclose(file_handle)
}
is it a good way or there are some better way ?
For something this simple I would use what I posted. With write_file()\read_file(), you do not open and close a file, you simply call the function and it does all of the work for you. There is no need to check if file exists since when you call write_file(), it will create a new file if one doesnt exist and write the line for you. If you still want to check if the file exists for some other reason, just use the file_exists() native.

You can access various lines in the file if you need to. If you notice, there is a 0 parameter specified which relates to the first line in the file. If you replace that with 1 it will work with the second line in the file, and so on.
__________________

Last edited by Bugsy; 08-09-2011 at 11:10.
Bugsy is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-09-2011 , 11:07   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #2

Depending your code. If your start by opening a file, you can check file_handle if > 0, if so it means the file exists.
__________________
Arkshine is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 08-09-2011 , 11:30   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #3

I am getting run time error while overwriting the 1st line of the file . Please help

error : [AMXX] Run time error 10: native error (native "write_file")

Code:
new handleDir = open_dir( OPENGL_DIR, s_FileName, charsmax( s_FileName ) );

	if(!file_exists(s_LastDll))
	{
		write_file(s_LastDll,s_FileName,0)
	}
	
	new s_Buffer[ 32 ] , iLen; 
	read_file( s_LastDll , 0 , s_Buffer , charsmax( s_Buffer ) , iLen )
	while( next_file( handleDir, s_Buffer, charsmax( s_Buffer ) ))
    {
        if(!(containi(s_Buffer,"dll") !=-1))
			continue
	
		// It will loop through all the found files.
        server_print( "Next Filename : %s", s_Buffer )
		write_file(s_LastDll,s_Buffer,0)
		break
    }

Last edited by gamer99; 08-09-2011 at 11:33.
gamer99 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 08-09-2011 , 11:32   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #4

That's because that file does not exist.
You have to add the directory you are looking in to the file to make the full path.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 08-09-2011 , 11:37   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #5

I had added

Code:
     new s_ConfigsDir[128],OPENGL_DIR[128],s_Temp[32]
       get_configsdir(s_ConfigsDir, sizeof(s_ConfigsDir)-1)
	format (OPENGL_DIR, sizeof(OPENGL_DIR)-1, "%s/opengl", s_ConfigsDir)
	format (s_LastDll, sizeof(s_LastDll)-1, "%s/opengl/lastopengl.dat", s_ConfigsDir)
it was before the above mentioned cade ..

Also the file created successfully 1st time through

Code:
if(!file_exists(s_LastDll))
	{
		write_file(s_LastDll,s_FileName,0)
	}
gamer99 is offline
gamer99
Senior Member
Join Date: Jul 2011
Old 08-09-2011 , 13:05   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #6

problem is solved . It was because of some permission problem for temp file . I deleted and recreated the file each time and that solved the problem
gamer99 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 08-09-2011 , 11:16   Re: read_file ( const file[], line, text[], len, &txtLen )
Reply With Quote #7

I just modified the function above to return values per Arkshines post.
__________________
Bugsy is offline
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 03:27.


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