SA-MP Forums Archive
fread for the win - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: fread for the win (/showthread.php?tid=71116)



fread for the win - Tannz0rz - 30.03.2009

Well, I'm trying to open a file and display it in game, and I'm trying to find proper methods to preform such.
Here is a method I have tried, but it doesn't properly display the file's content:
Код:
new ForumRead[32];
new filename[] = "forum.log", File: rFile = fopen("forum.log", io_read);
fread(rFile, filename, sizeof(filename));
format(ForumRead, sizeof(ForumRead), " %s ", filename);
SendClientMessage(playerid, COLOR_LIGHTBLUE, ForumRead);
If anyone knows how to properly open a file's content and display it, please do post it.


Re: fread for the win - joemomma53 - 30.03.2009

Quote:

If anyone knows how to properly open a file's content and display it, please do post it.

I use Dini.

http://forum.sa-mp.com/index.php?topic=981.0


Re: fread for the win - Tannz0rz - 30.03.2009

Well, any thus further help? I have found for some reason it's not the fact that it doesn't read the file's contents properly, it's just that it only reads like the first 9 or 10 letters of the file.

i.e. I have written "Thisshitdoesn'tworkright" in the file, and when I type the command, it only displays "Thisshitd".
Can anyone refer to my previous post and help improve the code?


Re: fread for the win - ICECOLDKILLAK8 - 30.03.2009

Try this
pawn Код:
new Contents[128];
new ForumRead[128];
new File:rFile = fopen("forum.log", io_read);
fread(rFile, Contents, sizeof(Contents));
format(ForumRead, sizeof(ForumRead), "%s", Contents);
SendClientMessage(playerid, COLOR_LIGHTBLUE, ForumRead);

// Or this for 1 less variable but IDK if it will work right, Try it :D

new ForumRead[128];
new File:rFile = fopen("forum.log", io_read);
fread(rFile, ForumRead, sizeof(ForumRead));
format(ForumRead, sizeof(ForumRead), "%s", ForumRead);
SendClientMessage(playerid, COLOR_LIGHTBLUE, ForumRead);



Re: fread for the win - Tannz0rz - 30.03.2009

Quote:
Originally Posted by JeNkStAX
Try this
pawn Код:
new Contents[128];
new ForumRead[128];
new File:rFile = fopen("forum.log", io_read);
fread(rFile, Contents, sizeof(Contents));
format(ForumRead, sizeof(ForumRead), "%s", Contents);
SendClientMessage(playerid, COLOR_LIGHTBLUE, ForumRead);

// Or this for 1 less variable but IDK if it will work right, Try it :D

new ForumRead[128];
new File:rFile = fopen("forum.log", io_read);
fread(rFile, ForumRead, sizeof(ForumRead));
format(ForumRead, sizeof(ForumRead), "%s", ForumRead);
SendClientMessage(playerid, COLOR_LIGHTBLUE, ForumRead);
Thanks a lot! The first one worked properly.


Re: fread for the win - ICECOLDKILLAK8 - 31.03.2009

Just so you know for next time, If a variable is gonna have a string read into it then it needs to be like this
pawn Код:
new Variable[128];
Every number in that variable means 1 character (i think),
So if you are gonna read a file that says "Hello World" then you would need to have a variable that is big enough for 11 chars e.g.
pawn Код:
new Variable[11];



Re: fread for the win - Donny_k - 31.03.2009

Just to add to the above:

The array is being asigned it's size by the length of the string you pass it ("forum.log") as you use '[]' and not a set size such as '[128]' or whatever you require.

pawn Код:
new filename[] = "forum.log"
Notice how these have the same amount of characters:

Quote:

"forum.log"
"Thisshitd"




Re: fread for the win - ICECOLDKILLAK8 - 31.03.2009

IDK why he even defined filename anyways lol, He used the full file name in fread


Re: fread for the win - Tannz0rz - 31.03.2009

Quote:
Originally Posted by JeNkStAX
IDK why he even defined filename anyways lol, He used the full file name in fread
Yeah, it gave me the error at first, and then I noticed that it wasn't even used. So I just got rid of it.
And I have another question. What do I do to display paragraphs in a file? So far, all this does is read the first line, but not the second. Any further help is appreciated.


Re: fread for the win - ICECOLDKILLAK8 - 31.03.2009

Take for instance a file reads this
Код:
Hello
World
In most cases if that file has been generated automaticlly then when you use fread that file would turn out as
Код:
Hello\nWorld
I think (Even though im not quiet sure) that if you tried to send that to a player using SendClientMessage it would only result in "Hello",
So unless you split each line at \n then send it line by line then you have no other soloution