Raised This Month: $51 Target: $400
 12% 

Trouble with reading first line of a file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
pillowCloud
Member
Join Date: Aug 2015
Location: AlliedModders
Old 08-04-2019 , 17:21   Trouble with reading first line of a file
Reply With Quote #1

Hi,
I had a trouble with reading the first line of a file, or to be more accurate, I found that the first character of the file is not something that appears on the actual file, for example:

my file looks like this:
PHP Code:
"pokemon" "pokemon.mp3"
"marko" "marko.mp3" 
but when I'm trying to print the first character of the file (witch shoud be "p"), it's gives me something else:


This causing me troubles when I'm trying to compare the SongName with a client input..
so here's my code:
PHP Code:
#include <amxmodx>

#define MAX_SONG_NAME 32
#define MAX_PATH_SIZE 64

#define CfgFile "addons/amxmodx/configs/cfg.txt"

new SongName[MAX_SONG_NAME];
new 
SongPath[MAX_PATH_SIZE];

new 
SongsLoaded;

public 
LoadSongs()
{
    if (
file_exists(CfgFile))
    {
        new 
text[64];
        new 
file fopen(CfgFile"r");
        
        while (!
feof(file))
        {
            
fgets(filetextcharsmax(text));
            
replace(textcharsmax(text), "^n""");
            if (
text[0] == ';' || !text[0]) continue;
            
            
console_print(0"[ test ] %c"text[0]);
            
console_print(0"[ test2 ] %s"text[0]);
            
            
parse(textSongNameMAX_SONG_NAMESongPathMAX_PATH_SIZE);
            
            
SongsLoaded++;
        }
        
        
fclose(file)
    }
    else
        
log_message("(Error) Failed to load file nostalgia.txt.");

Can someone explain me what am I doing wrong?
Thanks in advance.

Last edited by pillowCloud; 08-05-2019 at 01:08.
pillowCloud is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 08-04-2019 , 17:41   Re: Trouble with reading first line of a file
Reply With Quote #2

Set file reading flags to "rt" read text probably it's reading binaries
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
pillowCloud
Member
Join Date: Aug 2015
Location: AlliedModders
Old 08-05-2019 , 01:06   Re: Trouble with reading first line of a file
Reply With Quote #3

Quote:
Originally Posted by Natsheh View Post
Set file reading flags to "rt" read text probably it's reading binaries
I tried it also, but still same result

EDIT:
If I'm adding an empty line in head of the file, like this:
PHP Code:

"pokemon" "pokemon.mp3"
"marko" "marko.mp3" 
It's works fine,
it's looks like something in the head of the file is including something i can't read.

Last edited by pillowCloud; 08-05-2019 at 01:57.
pillowCloud is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-05-2019 , 06:26   Re: Trouble with reading first line of a file
Reply With Quote #4

Quote:
Originally Posted by pillowCloud View Post
I tried it also, but still same result

EDIT:
If I'm adding an empty line in head of the file, like this:
PHP Code:

"pokemon" "pokemon.mp3"
"marko" "marko.mp3" 
It's works fine,
it's looks like something in the head of the file is including something i can't read.
That's why you should always show the full code/config.
__________________
edon1337 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-06-2019 , 00:35   Re: Trouble with reading first line of a file
Reply With Quote #5

Don't do this:

Code:
replace(text, charsmax(text), "^n", "");
Do this:

Code:
trim(text)
Not sure this is related to your issue but you might also consider attaching the file you're trying to read so that we can look at it in our own editor and even test your code.
__________________

Last edited by fysiks; 08-06-2019 at 00:36.
fysiks is offline
pillowCloud
Member
Join Date: Aug 2015
Location: AlliedModders
Old 08-07-2019 , 02:16   Re: Trouble with reading first line of a file
Reply With Quote #6

Quote:
Originally Posted by edon1337 View Post
That's why you should always show the full code/config.
Quote:
Originally Posted by fysiks View Post
Don't do this:

Code:
replace(text, charsmax(text), "^n", "");
Do this:

Code:
trim(text)
Not sure this is related to your issue but you might also consider attaching the file you're trying to read so that we can look at it in our own editor and even test your code.
Firstable, thank you for trying to help,

I did managed to find the solution, to solve this issue I made 3 changes:
1. I changed the the read text property from "r" to "rt"
2. I removed the quotes from the line that I was read, by remove_all function.
3. MOST IMPORTANT: I created a brand new file encoded with ANSI (the old one was encoded by UTF-8, wich may explain why i wasn't been able to print the first character correctly).

I guess this issue was related to UTF-8 compatibility,
Anyway, thanks again for you all!
pillowCloud is offline
Reply



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 06:09.


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