Load a file from another file
#1

Hi..
I made a file where is other file names , and when i want to load files , the server call me as the file don't exist,I put a 'printf' and print in console this:
PHP код:
ERRORI can't open file: maps/aa
.m 
How i can delete that \r\n?
Reply
#2

I don't understand what you're saying.
Reply
#3

If i put the code you will understand
pawn Код:
stock LoadMaps()
{
    new string[256];
    new File:Handler=fopen("Maps.l",io_read);
    while(fread(Handler,string))
    {
        new str[256];
        format(str,256,"%s.m",string);
        if(!fexist(str))
        {
            printf("ERROR: I can't open file: %s",str);
            break;
        }
        //blabla
    }
    fclose(Handler);
    return 1;
}
file Maps.l:
PHP код:
Test 
And in console:
PHP код:
[22:11:29ERRORI can't open file: Test
.m 
Reply
#4

Код:
new File:Handler=fopen("Maps.l",io_read);
    while(fread(Handler,string))
    {
        new str[256];
        format(str,256,"%s.m",string);
What is the right format of file? *.l or *.m ??

and why you need new string[256]; 2x ?? use string[64]; and only once, Im sure you dont have any file name longer than 64 symbols
Reply
#5

I think this may be a SA:MP bug, although I'm not sure. I don't think it used to happen? Anyhow, in order to fix it you have to manually loop through the array and find the \r\n, once you find them then set them to 0 (string terminator)

e.g.

You could do something like this (untested) function to terminate the string at a new line, which should be fine for fread as it works line by line (i.e. \r\n)..

pawn Код:
TerminateNL(string[], start = 0, len = sizeof string)
{
    // It's assumed a string so we'll make sure it has a null terminator at the end.
    string[len-1] = 0;

    while(string[++start]) {
        if (string[start] == '\r' || string[start] == '\n')
            string[start] = 0; // You could put a "break;" statement here if you wish to not terminate all NL characters for efficiency. The string should "end" when it reaches the first string terminator.
    }
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)