Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (AndrewAMD, TipmyPip), 12,420 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
Re: Read a text file line by line [Re: PadMalcom] #342704
09/30/10 09:28
09/30/10 09:28
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
Originally Posted By: PadMalcom
@Stromausfall: Problem is that one line might contain "\n" in between not only at the end.


Then look for the "\r\n" escape sequence.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Read a text file line by line [Re: WretchedSid] #342706
09/30/10 09:41
09/30/10 09:41
Joined: Mar 2006
Posts: 1,993
Karlsruhe
PadMalcom Offline OP
Serious User
PadMalcom  Offline OP
Serious User

Joined: Mar 2006
Posts: 1,993
Karlsruhe
Good idea wink

Re: Read a text file line by line [Re: PadMalcom] #342949
10/02/10 12:39
10/02/10 12:39
Joined: Mar 2006
Posts: 1,993
Karlsruhe
PadMalcom Offline OP
Serious User
PadMalcom  Offline OP
Serious User

Joined: Mar 2006
Posts: 1,993
Karlsruhe
For all those who are interested in a solution:

Code:
STRING* file_read_line(var myHandle, int nLineNr) {
	if (!myHandle) return "";
	
	var oldPosition = file_seek(myHandle,0,4);
	STRING* res = str_create("#1000");
	
	// Find the right line
	file_seek(myHandle,0,0);
	
	int i;
	int j;
	for (i=0;i<=nLineNr;i++) {
		str_cpy(res,"");
		j = file_str_readto(myHandle,res, "\r\n", 1000);
		if (j == -1) {
			file_seek(myHandle,oldPosition,0);
			return "";
		}
		else
		{
			if (i == nLineNr) {
				file_seek(myHandle,oldPosition,0);
				return res;
			}
		}
	}
}



EDIT:
In addition a function to read the line count of a text file:
Code:
int file_count_lines(var myHandle) {
	if (!myHandle) return -1;
	
	var oldPosition = file_seek(myHandle,0,4);
	
	file_seek(myHandle,0,0);
	
	int res = 0;
	int j = 0;
	STRING* tempStr = str_create("#1000");
	while (1) {
		j = file_str_readto(myHandle,tempStr,"\r\n", 1000);
		if (j == -1) {
			break;
		}
		else
		{
			res +=1;
		}
	}
	file_seek(myHandle,oldPosition,0);
	return res;
}



Last edited by PadMalcom; 10/02/10 12:55.
Page 2 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1