Reading and printing out variables via fread +REP
#1

Hey,
I've been trying to make a unique system for my server. Basically it creates a file via the File Functions native and I'd like it to print out something from the very same file after it's done. The file itself contains two lines, the player's name and another string. How can I print out the 2nd line in the file?

Thanks in advance!
Reply
#2

Have you learnt the fseek(); function?
Code:
fseek(file name, long int offset, int whence);
Reply
#3

If they are split by lines (\r\n or \n) it's way easier to use just fread.

fread will read one line only, so if you do fread twice after opening, the string will contain the second line.
It will return 1 if there is content, 0 if not.

fseek is possible too, but you need to know the exact position of the starting point of line 2 (which is in your case length of playername plus 1 or 2 (depending on if you used "\n" or "\r\n")).
Reply
#4

I recommend fseek(); always!
It gives the EXACT address of the pointer variable.

Since fread(); comes handy only during (\n(s)) are used, why not use fseek(); always and get used to it? It will be handy when you have major saved string data's

But in this case as NaS said, you can use fread();
Reply
#5

Quote:
Originally Posted by Godey
View Post
I recommend fseek(); always!
It gives the EXACT address of the pointer variable.

Since fread(); comes handy only during (\n(s)) are used, why not use fseek(); always and get used to it? It will be handy when you have major saved string data's

But in this case as NaS said, you can use fread();
You're right, fseek is definitely better if the player's name (line 1) is given before opening the file, guess that depends on what system Amit planned to do.
Reply
#6

....
Reply
#7

Code:
ireason = fread(cfg, ireason); // over here
You can't use like this!
ireason is the variable stored in the Variable (I suggest using ireason[28])
How can you load a byte from a variable and save it to a same variable?
Reply
#8

Quote:
Originally Posted by Godey
View Post
Code:
ireason = fread(cfg, ireason); // over here
You can't use like this!
ireason is the variable stored in the Variable (I suggest using ireason[28])
How can you load a byte from a variable and save it to a same variable?
Yeah, I have noticed that. Hold on, I'll update you if there are any issues.
Reply
#9

......
Reply
#10

Code:
format(Line, sizeof(Line), Location, Code, ireason);
You forgot to add ireason in the format();!
Reply
#11

Quote:
Originally Posted by Godey
View Post
Code:
format(Line, sizeof(Line), Location, Code, ireason);
You forgot to add ireason in the format();!
It still prints out the location of the file rather than the actual content :/
Reply
#12

Try replacing Location with ireason?
Reply
#13

Quote:
Originally Posted by Godey
View Post
Try replacing Location with ireason?
I get this on the format line -> error 035: argument type mismatch (argument 3)
Reply
#14

Ok let it be like this.
Code:
format(Line, sizeof(Line), Location, Code);
Can you tell me exactly what's happening?
What's the output?
Reply
#15

It prints out the kick message like this:
Name: PlayersName
HC: PlayersGPCI
Reason: /HardwareBans/KLAJSD123098JHWHAJ.lock

When it should print the reason rather than the location.
Reply
#16

Ok see, by default ireason is saving as the pointer of ireason (that is the location!)
so you need to withdraw the particular reason from the file, not it's location.
Are you sure the file which you are loading (cfg) has the Reason: string value?
Reply
#17

Hum the file itself looks like this from inside:
"Amit_Godey"
"Hacking"

for e.g ofc
Reply
#18

Have you used fseek(); to jump to the second line and then read the string?
Reply
#19

Nope lol I couldn't understand the use of it. Mind editing it up for me or briefly explain what I should do? >:
Reply
#20

I'm not used to File I/O's but I will try to help you.
Try using
Code:
fseek (cfg, 28, SEEK_SET);
EDIT: btw why you used this 2 times?
Code:
ireason = fread(cfg, strinsg); // over here 
ireason = fread(cfg, strinsg); // over here
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)