Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 945 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Read a line from a .txt file? #470913
02/12/18 20:31
02/12/18 20:31
Joined: Sep 2017
Posts: 235
H
Hredot Offline OP
Member
Hredot  Offline OP
Member
H

Joined: Sep 2017
Posts: 235
I guess one could use a generic C code to read the first line from a txt file, as for instance described here.
Does Zorro by any chance have a more convenient function to accomplish this task?
Or, how would one read the LAST line in a .txt file?

Last edited by Hredot; 02/12/18 20:48.
Re: Read a line from a .txt file? [Re: Hredot] #470914
02/12/18 23:05
02/12/18 23:05
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Zorro has some handy file functions, which you can easily wrap with a helper function.
http://zorro-project.com/manual/en/file_.htm

But how you would wrap them depends on what you are trying to do.

For example, you can convert a file into a char* string and change the first newline character '\n' to a null character '\0' to get the first line.

For last line, simply find the last '\n' and point to the character after it.

I might be able to demo this first thing in the morning tomorrow unless someone beats me to it.

Re: Read a line from a .txt file? [Re: AndrewAMD] #470915
02/13/18 06:05
02/13/18 06:05
Joined: Sep 2017
Posts: 235
H
Hredot Offline OP
Member
Hredot  Offline OP
Member
H

Joined: Sep 2017
Posts: 235
@AndrewAMD
Awesome link, thanks! Looking forward to your demo!

Re: Read a line from a .txt file? [Re: Hredot] #470932
02/13/18 12:52
02/13/18 12:52
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Well, I didn't write helpers, but I left you a ton of clues on how to write helpers for your own use case.

See attached.

I suppose that if I had more time, I can write it into an identical style function, like "file_read_firstline" and "file_read_lastline".

Code:
int main()
{
	// create a file
	const string f = "./mumble.txt";
	file_write(f,"Many mumbling mice",0);
	file_append(f,"\nAre making merry music in the moonlight!",0);
	file_append(f,"\nMighty nice!",0);
	
	// check file length
	long l = file_length(f), l2 = 0;
	printf("\nlength: %d",l);
	
	// allocate memory for reading the file into a string
	char* str = malloc(l+1);
	memset(str,0,l+1);
	
	// fill the string with data and check how much data was copied
	l2 = file_read(f,str,l+1);
	printf("\nlength2: %d",l2);
	
	// let's see where the newline characters are
	int i = 0;
	for(i = 0; i<l; i++)
	{
		if(str[i]=='\n') printf("\ni: %d, found newline!",i);
	}
	// let's print the string
	printf("\n%s",str);
	
	// let's find the last newline character (if not found, zero)
	for(i = l-1; i>0; i--)
	{
		if(str[i]=='\n') {break;}
	}
	// point to the last part of the string by offsetting the pointer
	char* lastline = str + i;
	printf("%s",lastline);
	
	// okay, now let's alter the original string to convert it into the first line only.
	// Remember, a string is all data before a null character.
	for(i=0; i<l; i++)
	{
		if(str[i]=='\n') {str[i]=0;break;}
	}
	// let's print the altered string (first line)
	printf("\n%s",str);
	
	
	// always free memory after using malloc
	free(str);
	return 0;
}


Attached Files
mumble.c (68 downloads)
Re: Read a line from a .txt file? [Re: AndrewAMD] #470935
02/13/18 13:24
02/13/18 13:24
Joined: Sep 2017
Posts: 235
H
Hredot Offline OP
Member
Hredot  Offline OP
Member
H

Joined: Sep 2017
Posts: 235
@AndrewAMD:
Thanks for the code!

I wonder, does malloc remember how much memory was allocated originally, or does free(str); only free up memory up to the null character, which could potentially lead to a memory leak?

Isn't loading the entire file just to read one line a bit excessive? Perhaps some ideas presented here could help ease the workload.

Re: Read a line from a .txt file? [Re: Hredot] #470937
02/13/18 13:41
02/13/18 13:41
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Originally Posted By: Hredot
does free(str); only free up memory up to the null character, which could potentially lead to a memory leak?
No. You can use malloc and free for any kind of data, not just char arrays.
Quote:
Isn't loading the entire file just to read one line a bit excessive? Perhaps some ideas presented here could help ease the workload.
I was trying to help you solve your problem in five minutes or less. You're writing in C... why do you care about these optimizations? Are you reading gigabytes of data?

Re: Read a line from a .txt file? [Re: AndrewAMD] #470941
02/13/18 14:25
02/13/18 14:25
Joined: Sep 2017
Posts: 235
H
Hredot Offline OP
Member
Hredot  Offline OP
Member
H

Joined: Sep 2017
Posts: 235
I see, OK!

Oh, I don't know... Perhaps I don't have a good perception for what could cause slowdown in C. Coming from a more higher level languages, I get worried about that too much I guess. laugh

In any case, thank you for your help! This is very useful code!

Last edited by Hredot; 02/13/18 14:25.
Re: Read a line from a .txt file? [Re: Hredot] #470942
02/13/18 14:31
02/13/18 14:31
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Ah, I see. Yes, this is not python or R. wink


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1