wirate
wirate

Reputation: 695

Migrate random access file code to PHP

#include <fstream>
#include <iostream>

using namespace std;

bool find_in_file(char*);
void insert_in_file(char*);
inline bool isNull(char* word);

int main()
{
    char word[25];

    for(int i = 0; i < 10; i++)
    {
        cin >> word;

        if( find_in_file(word) )
            cout << "found" << endl;
        else
            insert_in_file(word);
    }
    system("pause");
}

bool find_in_file(char* word)
{
    ifstream file;
    file.open("file.dat", ios::in);
    char contents[655][25] = {0};


    file.read(reinterpret_cast<char*>(contents), 16*1024);
    file.close();

    int i = 0;

    while( !isNull(contents[i]) )
    {
        if( strcmp(contents[i], word) == 0)
            return true;

        if( strcmp(contents[i], word) < 0 )
            i = 2*i + 2;
        else
            i = 2*i + 1;
    }

    return false;
}

void insert_in_file(char* word)
{
    fstream file;
    file.open("file.dat", ios::in | ios::binary);
    char contents[655][25] = {0};

    file.read(reinterpret_cast<char*>(contents), 16*1024);
    file.close();


    file.open("file.dat", ios::in | ios::out | ios::binary);

    if( isNull(contents[0]) )
    {
        file.write(word, 25);
        file.close();
        return;
    }

    int parent;
    int current = 0;

    while( !isNull(contents[current]) )
    {
        parent = current;

        if( strcmp( contents[current], word ) < 0 )
            current = current*2 + 2;
        else if ( strcmp( contents[current], word ) > 0)
            current = current*2 + 1;
        else
            return;
    }

    int insertAt;

    if( strcmp(contents[parent], word ) < 0 )
        insertAt = parent*2 + 2;
    else
        insertAt = parent*2 + 1;

    file.seekp(insertAt*25, ios_base::beg);
    file.write(reinterpret_cast<const char*>(word), 25);
    file.close();
}

inline bool isNull(char* word)
{
    return word[0] == 0;
}

The above code implements a binary search tree on file. It uses char arrays of length 25 as nodes. It assumes a size of around 16K as max for the file. The tree is stored in this format:

0 root
1 left child of root - L
2 right child of root - R
3 left child of L - LL
4 right child of L - LR
5 left child of R - RL
6 right child of R - RR

and so on. In the absence of a child, an empty node is inserted. Now I have to do the same thing in PHP. How is it possible since as far as I know, PHP does not provide binary file access. Eagerly looking forward to your responses :)

edit: If I write an integer to file in binary mode, c/c++ will write 4 bytes regardless of the value stored in that integer. PHP will write the plain integer value in file, that is, 0 if the value is 0 and 100 if it is 100. This raises problems when using seek because I dont know the specific number of bytes to move the put pointer. Or in this case, I am writing character arrays of fixed length = 25. How can I do this in php since the variables dont have a type at all?

Upvotes: 0

Views: 312

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272657

PHP does provide binary file access. Use fopen() and specify 'b' in the mode field.

To perform random access (i.e. read/write), you should specify 'r+' in the mode field (or 'w+', 'x+' or 'a+', depending on precisely what you want to do).

To actually write binary data (rather than textual representations of that data), use fwrite() and pack().

Upvotes: 1

Deleteman
Deleteman

Reputation: 8700

From php documentation:

In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

What exactly do you mean when you say that php doesn't provide binary file access?

Upvotes: 0

Related Questions