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 948241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:12:42+00:00 2026-05-15T23:12:42+00:00

I don’t know C++ very well especially the IO part. Can anyone please help

  • 0

I don’t know C++ very well especially the IO part. Can anyone please help me to translate the following C++ code into C#?

unsigned *PostingOffset, *PostingLength, NumTerms;

void LoadSubIndex(char *subindex) {
  FILE *in = fopen(subindex, "rb");
  if (in == 0) {
    printf("Error opening sub-index file '%s'!\n", subindex);
    exit(EXIT_FAILURE);
  }
  int len=0;
  // Array of terms
  char **Term;
  char *TermList;
  fread(&NumTerms, sizeof(unsigned), 1, in);
  PostingOffset = (unsigned*)malloc(sizeof(unsigned) * NumTerms);
  PostingLength = (unsigned*)malloc(sizeof(unsigned) * NumTerms);
  Term = (char**)malloc(sizeof(char*) * NumTerms);
  Term = (char**)malloc(sizeof(char*) * NumTerms);
  // Offset of each posting
  fread(PostingOffset, sizeof(unsigned), NumTerms, in);
  // Length of each posting in bytes
  fread(PostingLength, sizeof(unsigned), NumTerms, in);
  // Number of bytes in the posting terms array
  fread(&len, sizeof(unsigned), 1, in); 
  TermList = (char*)malloc(sizeof(char) * len);
  fread(TermList, sizeof(unsigned)*len, 1, in);

  unsigned k=1;
  Term[0] = &TermList[0];
  for (int i=1; i<len; i++) {
    if (TermList[i-1] == '\0') {
      Term[k] = &TermList[i];
      k++;
    }
  }
  fclose(in);
}

Thanks in advance.

  • 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-15T23:12:43+00:00Added an answer on May 15, 2026 at 11:12 pm

    I’ll give you a headstart.

    using(var reader = new BinaryReader(new FileStream(subindex, FileMode.Open)) {
        int numTerms = reader.ReadUInt32();
        postingOffset = new UInt32[numTerms];
        postingLength = new UInt32[numTerms];
        var term = new byte[numTerms];
        for(int i=0;i<numTerms;i++)
            postingOffset[i] = reader.ReadUInt32();
        for(int i=0;i<numTerms;i++)
            postingLength[i] = reader.ReadUInt32();
        var len = reader.ReadInt32();
        var termList = new ... // byte[] or uint32[] ??
        //etc
    }
    

    There’s no need to close the file handle here – it will close when the using { } block loses scope.

    I didn’t finish it because there are some flaws in your code. With TermList you are reading in 4 times as much data as you’ve allocated. You shouldn’t be allocating Term twice either – that will result in leaking memory.

    To turn Term back into a string, use Encoding.ASCII.GetString(term).TrimEnd('\0');

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

Sidebar

Related Questions

Don't know if anyone can help me with this or if it's even possible.
Don't know why but font is not displaying.Please help. CSS(in css folder): style.css: @font-face
I don't know why, but this code worked for me a month ago... maybe
Don't be frightened, its a very basic code. Just wanted to check with you
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know why I can't reply to people on here with only a small
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user
Don't know how to frase this but I found this code wich works as

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.