Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6974001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:10:51+00:00 2026-05-27T17:10:51+00:00

#include <fstream> #include <iostream> using namespace std; bool find_in_file(char*); void insert_in_file(char*); inline bool isNull(char*

  • 0
#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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:10:52+00:00Added an answer on May 27, 2026 at 5:10 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <string> #include <fstream> using namespace std; int main() { char x[20];
#include <iostream> #include <fstream> #include <string> using namespace std; int main (int argc, char*
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
I have got the following sample: #include <iostream> #include <fstream> using namespace std; int
i wrote the following code.... #include< iostream> #include< fstream> using namespace std; int main()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.