Raised This Month: $32 Target: $400
 8% 

Solved File cannot be read correctly (only reads 1 character)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 04-11-2022 , 03:39   File cannot be read correctly (only reads 1 character)
Reply With Quote #1

Hello, i'm trying to implement a waypoint based pathfinding it's working but it's not saving correctly or not readed correctly i dont know, its ported from C++ version.

Code:
enum struct WaypointHeader
{
        char author[32];
        char editor[32];
        int fileVersion;
        int pointNumber;
}

enum struct Path
{
        int index;
        int activeArea;

        int flags;
        int radius;

        int distance[8];
        int pathIndex[8];

        int origin[3];
        int campStart[3];
        int campEnd[3];
}

// read the all waypoint data
for (int i = 0; i < m_waypointNumber; i++)
{
        fp.Read(m_paths[i].index, 1, 4);
        fp.Read(m_paths[i].activeArea, 1, 1);
        fp.Read(m_paths[i].flags, 1, 1);
        fp.Read(m_paths[i].radius, 1, 4);
        fp.Read(m_paths[i].distance, MaxPathIndex, 2);
        fp.Read(m_paths[i].pathIndex, MaxPathIndex, 2);
        fp.Read(m_paths[i].origin, 3, 4);
        fp.Read(m_paths[i].campStart, 3, 4);
        fp.Read(m_paths[i].campEnd, 3, 4);
}

// write about the file and author
fp.WriteString(header.author, false);
fp.Write(header.pointNumber, 1, 1);
fp.Write(header.fileVersion, 1, 1);

// write the all waypoint data
for (int i = 0; i < m_waypointNumber; i++)
{
        fp.Write(m_paths[i].index, 1, 4);
        fp.Write(m_paths[i].activeArea, 1, 1);
        fp.Write(m_paths[i].flags, 1, 1);
        fp.Write(m_paths[i].radius, 1, 4);
        fp.Write(m_paths[i].distance, MaxPathIndex, 2);
        fp.Write(m_paths[i].pathIndex, MaxPathIndex, 2);
        fp.Write(m_paths[i].origin, 3, 4);
        fp.Write(m_paths[i].campStart, 3, 4);
        fp.Write(m_paths[i].campEnd, 3, 4);
}
but always reads first character of waypoint author and writes the all data but only goes in 1 line

C++ writes and reads like (and not the all writed data goes on 1 line)
Code:
// write the waypoint header to the file...
fp.Write(&header, sizeof(header), 1);

// save the waypoint paths...
for (int i = 0; i < m_waypointNumber; i++)
     fp.Write(m_paths[i], sizeof(Path));

// read the waypoint header to the file...
fp.Read(&header, sizeof(header), 1);

// save the waypoint paths...
for (int i = 0; i < m_waypointNumber; i++)
     fp.Read(m_paths[i], sizeof(Path));
i don't know how to fix this, thanks.
__________________
CS 1.6 E-BOT (Best Bot For Zombie Plague): https://github.com/EfeDursun125/CS-EBOT

Last edited by EfeDursun125; 04-11-2022 at 19:10.
EfeDursun125 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-11-2022 , 03:46   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #2

...so this your code have not anything to do with SourcePawn ? SourceMod plugins ?
Bacardi is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 04-11-2022 , 03:48   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #3

Quote:
Originally Posted by Bacardi View Post
...so this your code have not anything to do with SourcePawn ? SourceMod plugins ?
no

EDIT: first code is made in sourcepawn
Attached Files
File Type: inc waypoints.inc (16.9 KB, 15 views)
__________________
CS 1.6 E-BOT (Best Bot For Zombie Plague): https://github.com/EfeDursun125/CS-EBOT

Last edited by EfeDursun125; 04-11-2022 at 03:49.
EfeDursun125 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-11-2022 , 04:15   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #4

ok, because you did not show whole code first (SourcePawn), not sure how you opened file.
Seems you opened file as normally.

Game have also called "KeyValues" structure, so I wanted to be sure which kind data we are handling.
- Lets forget this, for now.


There is sometimes problem with different Operation System (Linux vs Windows) and txt file, where "new line" bytes get mixed.

Example, if I remember right, you open txt file made on Linux, into Windows OS, you have all text in first line.
You are missing new line char.
https://www.commandlinewizardry.com/...ws-line-breaks

To make sure, your file work:
- Tell us which OS your server is ?
- Upload your path file in your post. With notepad++ can check which file format it is.
__________________
Do not Private Message @me
Bacardi is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 04-11-2022 , 04:29   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #5

Quote:
Originally Posted by Bacardi View Post
ok, because you did not show whole code first (SourcePawn), not sure how you opened file.
Seems you opened file as normally.

Game have also called "KeyValues" structure, so I wanted to be sure which kind data we are handling.
- Lets forget this, for now.


There is sometimes problem with different Operation System (Linux vs Windows) and txt file, where "new line" bytes get mixed.

Example, if I remember right, you open txt file made on Linux, into Windows OS, you have all text in first line.
You are missing new line char.
https://www.commandlinewizardry.com/...ws-line-breaks

To make sure, your file work:
- Tell us which OS your server is ?
- Upload your path file in your post. With notepad++ can check which file format it is.
It's all generated on Windows and file is not txt its saved as custom format, i think its reading the data incorrectly

file: data1data2data3
reads only: d (missing: ata1data2data3, but exists in file)

EDIT: when i save as text it fills with empty spaces
Attached Files
File Type: txt ctf_doublecross.txt (7.2 KB, 14 views)
__________________
CS 1.6 E-BOT (Best Bot For Zombie Plague): https://github.com/EfeDursun125/CS-EBOT

Last edited by EfeDursun125; 04-11-2022 at 04:39.
EfeDursun125 is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 04-11-2022 , 04:42   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #6

Code:
File fp = OpenFile(filepath, "r");
Per the docs, text mode is implied; you'll want to specify the mode as "rb" for binary reads and "wb" for binary writes.

Not sure if that is the root cause, but it's something I noticed in your file.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)
nosoop is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 04-11-2022 , 05:06   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #7

Quote:
Originally Posted by nosoop View Post
Code:
File fp = OpenFile(filepath, "r");
Per the docs, text mode is implied; you'll want to specify the mode as "rb" for binary reads and "wb" for binary writes.

Not sure if that is the root cause, but it's something I noticed in your file.
always triggering this

Code:
fp.Read(header.fileVersion, 1, 1);
if (header.fileVersion > WaypointVersion)
{
        PrintHintTextToAll("Waypoint version is too high\nPlease update your ebot!\nThis waypoint file will be ignored %d", header.fileVersion);
        fp.Close();
        return;
}
and %d is 255 i don't think its readed in correct way
__________________
CS 1.6 E-BOT (Best Bot For Zombie Plague): https://github.com/EfeDursun125/CS-EBOT
EfeDursun125 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-11-2022 , 06:05   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #8

...I think you gonna get more trouble by handling file on later.
I would suggest you to use KeyValues (and/or SMC parse to read).

But let's continue with this File structure. With BINARY mode (rb)

I encounter first problem with ReadString
Code:
    // read about the file and author // Note! (char author[32])
    WaypointHeader header;
    fp.ReadString(header.author, sizeof(header.author), sizeof(header.author));
...this start read all data as STRING from first line until it hits string limit (32) or NULL terminate, which is end of file.
Means, it read even new lines.
example
map.ewp
Code:
author is me
9
2
5
end
fp.ReadString(header.author, sizeof(header.author), sizeof(header.author)); output
Code:
author is me
9
2
5
end
... if AUTHOR name is less than 32 bytes you can't get correct version numbers.





Using ReadLine you can get one line at time.
Code:
author is me

But you need tell us in this point:
Are you gonna use waypoint files as normal TEXT file (so you can read with notepad)
Or are you gonna handle as binary file (this turn data to bytes so it is unreadable in notepad).

- How your waypoint file suppose to look at the end ?



*edit
Next you need replace Read -> https://sm.alliedmods.net/new-api/files/File/ReadInt8 or else

*edit
on stock void WaypointSave()
you need open file "a+"
__________________
Do not Private Message @me

Last edited by Bacardi; 04-11-2022 at 06:21.
Bacardi is offline
EfeDursun125
Senior Member
Join Date: Feb 2019
Location: Turkey
Old 04-11-2022 , 06:22   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #9

Quote:
Originally Posted by Bacardi View Post
...I think you gonna get more trouble by handling file on later.
I would suggest you to use KeyValues (and/or SMC parse to read).

But let's continue with this File structure. With BINARY mode (rb)

I encounter first problem with ReadString
Code:
    // read about the file and author // Note! (char author[32])
    WaypointHeader header;
    fp.ReadString(header.author, sizeof(header.author), sizeof(header.author));
...this start read all data as STRING from first line until it hits string limit (32) or NULL terminate, which is end of file.
Means, it read even new lines.
example
map.ewp
Code:
author is me
9
2
5
end
fp.ReadString(header.author, sizeof(header.author), sizeof(header.author)); output
Code:
author is me
9
2
5
end
... if AUTHOR name is less than 32 bytes you can't get correct version numbers.





Using ReadLine you can get one line at time.
Code:
author is me

But you need tell us in this point:
Are you gonna use waypoint files as normal TEXT file (so you can read with notepad)
Or are you gonna handle as binary file (this turn data to bytes so it is unreadable in notepad).

- How your waypoint file suppose to look at the end ?



*edit
Next you need replace Read -> https://sm.alliedmods.net/new-api/files/File/ReadInt8 or else
int origin[3];
error 035: argument type mismatch (argument 1)
fp.ReadInt8(m_paths[i].origin);

fp.ReadInt8(m_paths[i].origin[3]); is not working

i will use them as binary data
__________________
CS 1.6 E-BOT (Best Bot For Zombie Plague): https://github.com/EfeDursun125/CS-EBOT

Last edited by EfeDursun125; 04-11-2022 at 06:27.
EfeDursun125 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-11-2022 , 08:57   Re: File cannot be read correctly (only reads 1 character)
Reply With Quote #10

ok, I maybe found your two problems.

When you save file,
you not set NULL terminate on WriteString header.author
- Next time when you read string, it read long as next NULL appear, mostly from your first waypoint data with value 0.
Or if 0 value not appear first, read whole 32 bytes.
*edit
- When not set NULL fp.WriteString(header.author, false);, it add Horizontal Tab (0x09) or it is random byte :S


When you load file,
you ReadString, with size of header.author [32 bytes]
- You read author name with bunch of data same time.
To avoid this problem is set NULL terminate into strings and ReadString till first NULL terminate comes.


Code:
stock void WaypointLoad()
...
    File fp = OpenFile(filepath, "r");
...
    // read about the file and author
    WaypointHeader header;
    fp.ReadString(header.author, sizeof(header.author), -1);
    fp.Read(header.pointNumber, 1, 1);
...



stock void WaypointSave()
...
    File fp = OpenFile(filepath, "w");
...
    // write about the file and author
    fp.WriteString(header.author, true);
...
I edit those two lines.



If it still not work, then order of loading and save data maybe not work correctly...
Attached Files
File Type: inc waypoints.inc (16.8 KB, 2 views)
__________________
Do not Private Message @me

Last edited by Bacardi; 04-11-2022 at 09:03.
Bacardi is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode