AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Trouble with reading first line of a file (https://forums.alliedmods.net/showthread.php?t=317921)

pillowCloud 08-04-2019 17:21

Trouble with reading first line of a file
 
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:
https://i.imgur.com/EPuJ02j.png

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.

Natsheh 08-04-2019 17:41

Re: Trouble with reading first line of a file
 
Set file reading flags to "rt" read text probably it's reading binaries

pillowCloud 08-05-2019 01:06

Re: Trouble with reading first line of a file
 
Quote:

Originally Posted by Natsheh (Post 2661909)
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.

edon1337 08-05-2019 06:26

Re: Trouble with reading first line of a file
 
Quote:

Originally Posted by pillowCloud (Post 2661963)
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.

fysiks 08-06-2019 00:35

Re: Trouble with reading first line of a file
 
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.

pillowCloud 08-07-2019 02:16

Re: Trouble with reading first line of a file
 
Quote:

Originally Posted by edon1337 (Post 2661993)
That's why you should always show the full code/config.

Quote:

Originally Posted by fysiks (Post 2662132)
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!


All times are GMT -4. The time now is 17:22.

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