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

  • Home
  • SEARCH
  • 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 8409285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:51:58+00:00 2026-06-09T23:51:58+00:00

When I output the return value of the InitialSeedFinder function seen below (aka value

  • 0

When I output the return value of the InitialSeedFinder function seen below (aka value stored in the seed variable), I get some random ascii chars that corrupt the expected string value. This only happens if the buffer is more 2 chars (ie. it works for when the order variable is less than 3).

This error is being introduced in the while loop seen in the code below…

Can someone explain WHY this is happening? does it have something to do with how the read() function works?

string InitialSeedFinder(int order, string fileName){
    string seed; 
    ifstream inputStream;
    Map<string, int> frequencyMap;
    inputStream.open(fileName.c_str());
    int offset = 0;
    inputStream.clear();
    char* buffer = new char [order];

    //get all k char sequence
    while (inputStream.get() != EOF) {    
        inputStream.seekg(offset);
        inputStream.read(buffer, order);
        string key(buffer);
        if (frequencyMap.containsKey(key)) {
            frequencyMap[key] = frequencyMap[key] + 1;
        }
        else {
            frequencyMap.put(key, 1);
        } 
         offset++;
    }
    inputStream.close();

  //go through and find the most frequent key
    int greatestFrequency = 0;
    int frequency = 0;
    foreach(string key in frequencyMap)
    {
        frequency = frequencyMap[key];
        if (frequency > greatestFrequency) {
            greatestFrequency = frequencyMap[key];
            seed = key;
        }
    }

    return seed;
}
  • 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-06-09T23:52:00+00:00Added an answer on June 9, 2026 at 11:52 pm

    read() does not add a terminator to the end if the string. However, when casting a char* to a string it requires a nul terminator. When your buffer was short you got lucky and there was a zero at the end, when it was longer there was non-zero data.

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

Sidebar

Related Questions

In the example code below, I'd like to get the return value of the
I am trying to get the return value of a stored procedure. Here is
I found this issue with distanceFromLocation function where the return value is not accurate.
Looking for suggestions on cleanest way to get the return value and the result
I am using the pow function in C and storing the return value in
I have very basic question regarding return value of a function, and checking the
I'm trying to retrieve the return value of a stored procedure in SQL Server
How do I find out in Toad what the return value of a function
A class implements a public function that calculates some value. During the calculation the
In my program I am trying to return a value from a function, the

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.