Forum

> > CS2D > Scripts > Read File simplification
Forums overviewCS2D overview Scripts overviewLog in to reply

English Read File simplification

4 replies
To the start Previous 1 Next To the start

old Read File simplification

Quattro
GAME BANNED Off Offline

Quote
Hey, is this the best way to set path and use it? Maybe there is a nicer way instead of .. ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
HomeDir = 'C:/my path to the file/

test = read_file(HomeDir..'test.txt')
msg(test)


function read_file(path)
	local con = 0
	local file = io.open(path, "rb")
	if not file then msg('©255100100Internal error: Could not read "'..path..'"') return con end
	con = file:read("*all")
	file:close()
	return con
end

old Re: Read File simplification

Yates
Reviewer Off Offline

Quote
Depends. Where is the file going to be located? If it's always going to be in the CS2D main folder or the parent, better off going back with
../../
instead of writing the full path.

Not sure if the Windows dedicated supports it or not though. Might even have to use backslashes.

old Re: Read File simplification

VADemon
User Off Offline

Quote
Forward and backslashes are handled at NTOS kernel level by Windows. There should be no difference for user-type applications.

@user Quattro: if you don't like the idea of having HomeDir variables everywhere in your code, you don't have to define that path in code at all.
You can create symlinks to directories outside of CS2D folder!

I use git for development, and it'd be a nuisance to copy files between CS2D and git folder or, even worse, to have the git folder inside cs2d. Symlinks to the rescue:
1
2
3
4
5
Directory of .\Counter-Strike 2D\sys\lua\autorun
           290 itemcollect.lua
<SYMLINK>      mapexport.lua [A:\git\cs2d-script\mapexport.lua]
<SYMLINK>      pp.lua [A:\git\cs2d-script\pure_pwnage.lua]
           357 Readme.txt
The program will then access /sys/lua/mapexport.lua ... and open the actual file at A:\git\cs2d-script\mapexport.lua - no editing required, File System stuff is done at file system level, not in configs.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview