How to read specific line from a file?
#1

Read #5 post in this thread....


Hello all, Im trying to read a specific line from file "interiors" (Grand Larceny file by Kye). Once you do /createhouse [Interior ID] It will search for Uniq Interior ID in that file and read other numbers like Xpos Ypos Zpos etc:
pawn Код:
CMD:createhouse(playerid, params[])
{
    new hinterior, hcost;
    if(sscanf(params, "dd", hinterior, hcost)) return scm(playerid, colorusage, "USAGE: /createhouse [Interior] [Price]");
    else if(pInfo[playerid][pAdmin] < 4) return SendClientMessage(playerid, colorgrey, "<!> You're not authorized to use that command.");
    else
    {
        new Float:X, Float:Y, Float:Z;
        GetPlayerPos(playerid, X, Y, Z);
            //   It should do it here... Read Interior, XPOS, YPOS etc from that line and put it into my variables

    }
    return 1;
}
example:

I write in /createhouse 127 [and price here], and command should read from line 127 and get info about Rosenberg's office in caligulas:
Quote:

126 6 442.1295 -52.4782 999.7167 177.9394 Secret Valley Diner ;
127 2 2182.2017 1628.5848 1043.8723 224.8601 Rosenberg's Office in Caligulas ;
128 6 748.4623 1438.2378 1102.9531 0.6069 Fanny Batter's Whore House ;
129 8 2807.3604 -1171.7048 1025.5703 193.7117 Colonel Furhberger's ;
130 9 366.0002 -9.4338 1001.8516 160.528 Cluckin' Bell ;

1st number "127" is Unique Interior id, second number is Interior ID, next 3 numbers are X,Y and Z cords, and last number is Facing Angle once you get in that interior.
Reply
#2

pawn Код:
new File:HouseFile = fopen(filename_here, io_read);
if(!HouseFile) return file bla bla doesnt exist
new string[128], unique, interior, Float:Pos[4], name[64];
while(fread(HouseFile, string))
{
    if(strval(string) == hinterior)
    {
        sscanf(string,"iia<f>[4]p<;>s[64]",unique, interior, Pos, name);
        break;
    }
}
fclose(HouseFile);
Reply
#3

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
new File:HouseFile = fopen(filename_here, io_read);
if(!HouseFile) return file bla bla doesnt exist
new string[128], unique, interior, Float:Pos[4], name[64];
while(fread(HouseFile, string))
{
    if(strval(string) == hinterior)
    {
        sscanf(string,"iia<f>[4]p<;>s[64]",unique, interior, Pos, name);
        break;
    }
}
fclose(HouseFile);
Thank you, but it will read whole file then, I need just one line, depending on ID you write in CMD "/createhouse ID" and that ID will navigate you to the line I want to read. (Unique ID)
Reply
#4

No, it will stop reading once you 'break' it, as shown above.

This is a loop:
Код:
while(fread(HouseFile, string))
This stops (breaks) a loop:
Код:
break;
:l
Reply
#5

It does not reading from the file. Everything I tried to get from a file was 0.0 -.-
pawn Код:
CMD:createhouse(playerid, params[])
{
    new hinterior, hcost, HID;
    if(sscanf(params, "ddd", HID, hinterior, hcost)) return scm(playerid, colorusage, "USAGE: /createhouse [HouseID] [Interior] [Price]");
    else if(pInfo[playerid][pAdmin] < 4) return SendClientMessage(playerid, colorgrey, "<!> You're not authorized to use that command.");
    else if(hinterior < 0 || hinterior > 150) return scm(playerid, colorgrey, "<!> Invalid interior ID!");
    else
    {
        if(fexist(HousePath(HID))) return scm(playerid, colorgrey, "<!> House with that ID already exist!");
        new Float:X, Float:Y, Float:Z, pInter, pVirW;
        GetPlayerPos(playerid, X, Y, Z);
        pInter = GetPlayerInterior(playerid);
        pVirW = GetPlayerVirtualWorld(playerid);
        CreateHouse(HID, hinterior, X, Y, Z, pInter, pVirW, hcost);
    }
    return 1;
}
pawn Код:
stock CreateHouse(HouseID, Unique, Float:X, Float:Y, Float:Z, OutsideInter, OutsideVirW, HousePrice)
{
    new File:HouseFile = fopen("HouseInteriors.cfg", io_read);
    new string[128], uniqueint, hinterior, Float:Pos[3], name[128];
    while(fread(HouseFile, string))
    {
        if(strval(string) == Unique)
        {
            sscanf(string,"iia<f>[3]p<;>s[64]",uniqueint, hinterior, Pos[0], Pos[1], Pos[2], name);
            break;
        }
    }
    fclose(HouseFile);

    new INI:House = INI_Open(HousePath(HouseID));
    INI_WriteFloat(House, "EnterX", X);
    INI_WriteFloat(House, "EnterY", Y);
    INI_WriteFloat(House, "EnterZ", Z);
    INI_WriteFloat(House, "ExitX", Pos[0]);
    INI_WriteFloat(House, "ExitY", Pos[1]);
    INI_WriteFloat(House, "ExitZ", Pos[2]);
    INI_WriteInt(House, "InsideInt", hinterior);
    INI_WriteInt(House, "InsideVW", HouseID+2);
    INI_WriteInt(House, "OutsideInt", OutsideInter);
    INI_WriteInt(House, "OutsideVW", OutsideVirW);
    INI_WriteInt(House, "Owned", 0);
    INI_WriteInt(House, "Price", HousePrice);
    INI_Close(House);
    printf("HouseID %d | UniqInteriorID: %d | Interior: %d | X: %f | Y: %f | Z: %f | OutsideInt: %d | OutsideVW: %d | Price: %d", HouseID, uniqueint, hinterior, Pos[0], Pos[1], Pos[2], OutsideInter, OutsideVirW, HousePrice);

    LoadHouse(HouseID, HousePath(HouseID));

    return 1;
}
Once I tried to load from HouseInteriors.INI, server crashed. Then I tried from .cfg file and server did not crashed but not loaded line properly. All I got from a file was 0.0. This is how my HouseInteriors.cfg file looks like:
Quote:

0 11 2003.1178 1015.1948 33.008 351.5789 Four Dragons' Managerial Suite ;
1 5 770.8033 -0.7033 1000.7267 22.8599 Ganton Gym ;
2 3 974.0177 -9.5937 1001.1484 22.6045 Brothel ;
3 3 961.9308 -51.9071 1001.1172 95.5381 Brothel2 ;
4 3 830.6016 5.9404 1004.1797 125.8149 Inside Track Betting ;
5 3 1037.8276 0.397 1001.2845 353.9335 Blastin' Fools Records ;
6 3 1212.1489 -28.5388 1000.9531 170.5692 The Big Spread Ranch ;
7 18 1290.4106 1.9512 1001.0201 179.9419 Warehouse 1 ;
8 1 1412.1472 -2.2836 1000.9241 114.661 Warehouse 2 ;
9 3 1527.0468 -12.0236 1002.0971 350.0013 B Dup's Apartment ;
10 2 1523.5098 -47.8211 1002.2699 262.7038 B Dup's Crack Palace ;
11 3 612.2191 -123.9028 997.9922 266.5704 Wheel Arch Angels ;
12 3 512.9291 -11.6929 1001.5653 198.7669 OG Loc's House ;
13 3 418.4666 -80.4595 1001.8047 343.2358 Barber Shop ;
14 3 386.5259 173.6381 1008.3828 63.7399 Planning Department ;
ETC...

Reply
#6

pawn Код:
CMD:createhouse(playerid, params[])
{
    new hinterior, hcost, HID;
    if(sscanf(params, "ddd", HID, hinterior, hcost)) scm(playerid, colorusage, "USAGE: /createhouse [HouseID] [Interior] [Price]");
    else if(pInfo[playerid][pAdmin] < 4) SendClientMessage(playerid, colorgrey, "<!> You're not authorized to use that command.");
    else if(!(0 <= hinterior <= 150)) scm(playerid, colorgrey, "<!> Invalid interior ID!");
    else
    {
        if(fexist(HousePath(HID))) return scm(playerid, colorgrey, "<!> House with that ID already exist!");
        new Float:X, Float:Y, Float:Z, pInter, pVirW;
        GetPlayerPos(playerid, X, Y, Z);
        pInter = GetPlayerInterior(playerid);
        pVirW = GetPlayerVirtualWorld(playerid);
        CreateHouse(HID, hinterior, X, Y, Z, pInter, pVirW, hcost);
    }
    return 1;
}

stock CreateHouse(HouseID, Unique, Float:X, Float:Y, Float:Z, OutsideInter, OutsideVirW, HousePrice)
{
    new File:HouseFile = fopen("HouseInteriors.cfg", io_read);
    new string[128], hinterior, Float:Pos[3];
    while(fread(HouseFile, string))
    {
        if(strval(string) == Unique)
        {
            sscanf(string,"{i}ia<f>[3]{s[64]}",hinterior, Pos);
            break;
        }
    }
    fclose(HouseFile);

    new INI:House = INI_Open(HousePath(HouseID));
    INI_WriteFloat(House, "EnterX", X);
    INI_WriteFloat(House, "EnterY", Y);
    INI_WriteFloat(House, "EnterZ", Z);
    INI_WriteFloat(House, "ExitX", Pos[0]);
    INI_WriteFloat(House, "ExitY", Pos[1]);
    INI_WriteFloat(House, "ExitZ", Pos[2]);
    INI_WriteInt(House, "InsideInt", hinterior);
    INI_WriteInt(House, "InsideVW", Unique+1);
    INI_WriteInt(House, "OutsideInt", OutsideInter);
    INI_WriteInt(House, "OutsideVW", OutsideVirW);
    INI_WriteInt(House, "Owned", 0);
    INI_WriteInt(House, "Price", HousePrice);
    INI_Close(House);
    printf("HouseID %d | UniqInteriorID: %d | Interior: %d | X: %.4f | Y: %.4f | Z: %.4f | OutsideInt: %d | OutsideVW: %d | Price: %d", HouseID, Unique, hinterior, Pos[0], Pos[1], Pos[2], OutsideInter, OutsideVirW, HousePrice);

    LoadHouse(HouseID, HousePath(HouseID));

    return 1;
}
Reply
#7

Same, Still not loading.
here's printf line after cmd /createhouse 3 106 30000

Quote:

HouseID 3 | UniqInteriorID: 106 | Interior: 0 | X: 0.0000 | Y: 0.0000 | Z: 0.0000 | OutsideInt: 0 | OutsideVW: 0 | Price: 30000

and here's saved file of House ID 3

Quote:

EnterX = 2122.727539
EnterY = -1004.087890
EnterZ = 58.633750
ExitX = 0.000000
ExitY = 0.000000
ExitZ = 0.000000
InsideInt = 0
InsideVW = 107
OutsideInt = 0
OutsideVW = 0
Owned = 0
Price = 30000

Reply
#8

Any solutions?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)