fread for the win
#1

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.
Reply
#2

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
Reply
#3

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?
Reply
#4

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);
Reply
#5

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.
Reply
#6

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];
Reply
#7

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"

Reply
#8

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

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.
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)