Jump to content
Tuts 4 You

How to read a text line by line / CRLF?


LCF-AT

Recommended Posts

Hi again,

does anyone know any function/s to split a text file (any text of notepad etc) into single lines by CRLF?

Example: I have simple textfile which has 10 lines (each line with return at the end / CRLF) and now I read the file into buffer via ReadFile API and now I got the whole text in buffer and now I need to check manually for CRLF at the end or not if not present etc.So is there any simple function/s etc I could use to get a entire line by line back?Maybe there is anything I could use and  dont know yet.

Thank you

Link to comment

In C++ I use "getline"

	//Open the text file
	ifstream file;
	file.open("C:\\sequence.txt");
 
	if (!file.is_open())
	{
		printf("error while opening file\n");
		system("Pause");
		return 1;
	}
 

	//Read the file line by line
	string line;
	for(int i = 0; i < 1000; i++)
	{
		getline(file,line);
	}

 

  • Like 1
Link to comment

Hi,

thanks for your answer but I need some usable (assembly / API etc) example. :) So is it possible to translate this part anyhow?

greetz

Link to comment

its hardly rocket science, readline is just a high level abstract -> https://dev.w3.org/libwww/Library/src/vms/getline.c

make buffer -> read buffer size bytes from file -> process until \r \n or \0, if no ending is found, reallocate the buffer 2x the size, loop, repeat ad nauseaum... there's no 'magic' win api to do it

and to save the i/o overhead and memory allocation and reallocation, depending on the size, it might just be best to memory map the bloody thing

also bear in mind for linux the 0d 0a is the other way round...and then of course, exotic files..utf-8 and so on.. joy

Edited by evlncrn8
  • Like 2
Link to comment

Hi again guys,

thanks for the infos and also thanks for the API example CodeCracker. :) So thats good so far with that msvcrt API. :) So now I dont need to handle that manually as I did before.

greetz

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...