Read a text file line by line

Posted By: PadMalcom

Read a text file line by line - 09/29/10 15:24

Hi, I have a text file and want to read one line after another into a String. The lines will contain characters like "\n" etc. Any ideas?
Posted By: Stromausfall

Re: Read a text file line by line - 09/29/10 16:15

hmmm file_str_readto seems to do what you want ! it even has a delimiter -> which you can set to '\n' !
Posted By: Germanunkol

Re: Read a text file line by line - 09/29/10 16:24

Why not use txt_load(TEXT*,STRING* filename) and then iterate through the strings and fill thim into one string?
Posted By: Razoron

Re: Read a text file line by line - 09/29/10 17:50

Write a DLL and use getline().
Posted By: WretchedSid

Re: Read a text file line by line - 09/29/10 18:25

Originally Posted By: Razoron
Write a DLL and use getline().

And I thought Lite Foundation would be the overkill, but your solution beats everything oO
Posted By: Razoron

Re: Read a text file line by line - 09/29/10 18:34

STL vs. Lite-C
STL wins!
Posted By: WretchedSid

Re: Read a text file line by line - 09/29/10 18:39

No, the shortest and easiest way to achieve your target wins. You solution is neither short nor easy.
Posted By: Razoron

Re: Read a text file line by line - 09/29/10 19:10

But it has the best performance.
Posted By: WretchedSid

Re: Read a text file line by line - 09/29/10 19:14

No, it doesn't. Mapping the DLL into the memory, calling the function etc. will cost much more than reading those few lines directly through Gamestudio.
Posted By: PadMalcom

Re: Read a text file line by line - 09/30/10 09:08

I agree, the point that I want to write such a function is to avoid using DLLs which would be no problem but not pretty nice.

@Stromausfall: Problem is that one line might contain "\n" in between not only at the end.

@Germanunkol: I think your solution is the best even if it is not as beautiful as I wanted.
Posted By: WretchedSid

Re: Read a text file line by line - 09/30/10 09:28

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.
Posted By: PadMalcom

Re: Read a text file line by line - 09/30/10 09:41

Good idea wink
Posted By: PadMalcom

Re: Read a text file line by line - 10/02/10 12:39

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;
}


© 2024 lite-C Forums