Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (degenerate_762, AbrahamR, AndrewAMD, ozgur), 667 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 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 | chip programmers | 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