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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:44:44+00:00 2026-06-17T12:44:44+00:00

This code is a part of a larger code that indexes files, and tokenizes

  • 0

This code is a part of a larger code that indexes files, and tokenizes the words in each file so that you can be able to search a certain word in the large amount of file you have. (like Google)

This function is supposed to search your files for a word that you want to find. But I don’t completely understand how it works!

Can someone please explain what this code does and how it does it?

In addition, I have several questions:
1) What exactly in “infile”?
2) What does the built-in function c_str() do?
3) Why does the variable “currentlineno” start at 1? Couldn’t the first line in a file start at 0?
4) What is the difference between ++x and x++?
5) What is the difference between the condition “currentlineno < lineNumber” and “currentlineno != lineNumber” ?

This is the code:

void DisplayResult(string fileName, int lineNumber)
{
ifstream infile(fileName.c_str(), ifstream::in);

char line[1000];
int currentlineno = 1;

while(currentlineno < lineNumber) 
{
    infile.getline(line, 1000);
    ++currentlineno;
}
infile.getline(line, 1000);
cout<<endl<<"\nResult from ("<<fileName<<" ), line #"<<lineNumber<<": "<<endl;
cout<<"\t"<<line;

infile.close();
}
  • 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-17T12:44:45+00:00Added an answer on June 17, 2026 at 12:44 pm

    This function display the line at the corresponding line number pass by parameter.

    1/ Infile permits to open a file as in put streams : http://www.cplusplus.com/reference/fstream/ifstream/

    2/ c_str() permits to pass to a string structure to a simple char* (a char array). It is the structure use in the language C, which explains why the method name is “c_str”. In C++, we usually use string more than char* cause it is really simpler.

    3/ Why currentlineno start at 1 ? The function read the file content before the given line number. The, read one more time to display the wanted line.

    4/ ++x is pre-incrementation, x++ is post-incrementation.
    When you use ++x, x is incremented before to use it, otherwise, with x++, x is incremented after.

    int x = 1;
    cout << ++x; // display 2
    x = 1;
    cout << x++; // display 1
    

    5/ Look at operators : http://www.cplusplus.com/doc/tutorial/operators/

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

Sidebar

Related Questions

This code snippet is just a small part of a larger php file. Currently,
I want to code this part of a VB6 application in c#. How can
Does your editor/ide highlight that a++; in this C/C++ code as part of a
I have some AppleScript code that I'm executing with osascript . This is part
It's a part of larger code base, which forces -Werror on gcc. This warning
Strange problem happening where the end of these 2 blocks of code (this part:
I have this code to run a slideshow (this is only part of what
What is this code doing? Specifically the default(XX) part. I've never seen it before.
I have this part of code in my functions.php: function cc_admin_enqueue_scripts($hook) { $file_dir=get_bloginfo('template_directory'); wp_enqueue_script('media-upload');
I am trying to learn and writting this part of code. while testing few

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.