简体   繁体   中英

searching files?

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) What exactly in "infile"?

ANS:: Construct object and optionally open file. Link

2) What does the built-in function c_str() do?

ANS:: It is needed to get a const char* representation of the text stored inside a std::string class. Link

3) Why does the variable "currentlineno" start at 1? Couldn't the first line in a file start at 0?

ANS:: Depends on the second input parameter of the function DisplayResult .

4) What is the difference between ++x and x++?

ANS:: See this . Probably you may have heard of Post-Increment and Pre-Increment.

5) What is the difference between the condition "currentlineno < lineNumber" and "currentlineno != lineNumber" ?

ANS:: Value of currentlineno should not exceed the value of lineNumber when condition is currentlineno < lineNumber . Value of currentlineno may exceed or may be less than the value of lineNumber but should not be equal to the value of lineNumber when condition is currentlineno != lineNumber .

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/

This function does not search for words.

It takes as input a file name and a line number. It tries to find and read that line.

The output starts with a line stating: "The result from ( fileName ), line # lineNumber : " It is followed by a text indented by a tab and followed by the found line contents. This second line of output is left incomplete (not followed by a newline).

The found contents is empty, if the file has has less than the requested number of lines or if any of the lines before the requested line has more than 999 characters. If the requested line has more than 999 characters it is truncated to 999 characters.

Other questions:

1) infile is a function-scope object of automatic storage duration and type std::basic_ifstream<char, std::char_traits<char>> , which is initialized for reading from the file named in fileName .

2) The member function c_str() built into the standard library string class returns a pointer to the string contents as a non-modifiable, nul-terminated character array, which is the format typically used in C for strings (type const char * ). For historical reasons the file-based standard library streams take their file name arguments in this format.

3) Humans typically count line numbers starting with one. That is the convention used for the lineNumber parameter. The algorithm used must match this. The currentlineno local variable is used to mean 'the number of the next line to be read'. As such it must be initialized with 1 . (This is somewhat confusing, considering the name of the variable.) Other implementations that initialize the line counter with 0 are possible - and indeed natural to most C++ programmers.

4) See any textbook or online reference of C++. Look for "pre-increment" ( ++x ) and "post-increment" ( x++ ) operators. They have the same side effect (increment x), but differ in the value of the expression. If you don't use the result they are equivalent (for basic types). C++ programmers usually prefer pre-increment as it can generally be implemented more efficiently for user-defined types.

5) Even more basic textbook question. a < b tests for a less-than relationship, a != b tests for inequality.

Note: All answers assume that the types used are from the standard C++ library, ie that appropriate includes of the <string> and <iostream> headers and necessary using directives or declarations are used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM