简体   繁体   English

搜索文件?

[英]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) (如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"? 另外,我有几个问题:1)“ infile”中到底是什么? 2) What does the built-in function c_str() do? 2)内置函数c_str()的作用是什么? 3) Why does the variable "currentlineno" start at 1? 3)为什么变量“ currentlineno”从1开始? Couldn't the first line in a file start at 0? 文件的第一行不能从0开始吗? 4) What is the difference between ++x and x++? 4)++ x和x ++有什么区别? 5) What is the difference between the condition "currentlineno < lineNumber" and "currentlineno != lineNumber" ? 5)条件“ currentlineno <lineNumber”和“ 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"? 1)“ infile”中到底是什么?

ANS:: Construct object and optionally open file. ANS ::构造对象并选择打开文件。 Link 链接

2) What does the built-in function c_str() do? 2)内置函数c_str()的作用是什么?

ANS:: It is needed to get a const char* representation of the text stored inside a std::string class. ANS ::需要获取存储在std :: string类中的文本的const char *表示形式。 Link 链接

3) Why does the variable "currentlineno" start at 1? 3)为什么变量“ currentlineno”从1开始? Couldn't the first line in a file start at 0? 文件的第一行不能从0开始吗?

ANS:: Depends on the second input parameter of the function DisplayResult . ANS ::取决于函数DisplayResult的第二个输入参数。

4) What is the difference between ++x and x++? 4)++ x和x ++有什么区别?

ANS:: See this . ANS ::看这个 Probably you may have heard of Post-Increment and Pre-Increment. 可能您可能听说过后增量和前增量。

5) What is the difference between the condition "currentlineno < lineNumber" and "currentlineno != lineNumber" ? 5)条件“ currentlineno <lineNumber”和“ currentlineno!= lineNumber”之间有什么区别?

ANS:: Value of currentlineno should not exceed the value of lineNumber when condition is currentlineno < lineNumber . ANS ::当条件为currentlineno < lineNumber时, 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 . 当condition为currentlineno != lineNumber时, currentlineno值可以大于或小于lineNumber的值,但不应等于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/ 1 / Infile允许按照输入流打开文件: http ://www.cplusplus.com/reference/fstream/ifstream/

2/ c_str() permits to pass to a string structure to a simple char* (a char array). 2 / c_str()允许将字符串结构传递给简单的char *(char数组)。 It is the structure use in the language C, which explains why the method name is "c_str". 这是语言C中使用的结构,它解释了方法名称为何为“ c_str”的原因。 In C++, we usually use string more than char* cause it is really simpler. 在C ++中,我们通常使用的字符串比char *多,因为它确实更简单。

3/ Why currentlineno start at 1 ? 3 /为什么currentlineno从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. 4 / ++ x是前递增的,x ++是后递增的。 When you use ++x, x is incremented before to use it, otherwise, with x++, x is incremented after. 当您使用++ x时,x会在使用前先递增,否则,对于x ++,x会在之后递增。

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

5/ Look at operators : http://www.cplusplus.com/doc/tutorial/operators/ 5 /查看运营商: 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. 输出以以下行开头:“( fileName )的结果,行lineNumber :”之后是带有制表符的缩进文本和找到的行内容。 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. 如果文件的行数少于请求的行数,或者在请求的行之前的任何行中的行数超过999个字符,则找到的内容为空。 If the requested line has more than 999 characters it is truncated to 999 characters. 如果请求的行超过999个字符,则将其截断为999个字符。

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 . 1) infile是具有自动存储持续时间的功能范围对象,类型为std::basic_ifstream<char, std::char_traits<char>> ,该文件初始化为从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 * ). 2)内置于标准库字符串类中的成员函数c_str()返回一个指向字符串内容的指针,该指针为不可修改的,以n终止的字符数组,这是C语言中通常用于字符串的类型(类型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. 3)人们通常以行号开始计数。 That is the convention used for the lineNumber parameter. 那是用于lineNumber参数的约定。 The algorithm used must match this. 使用的算法必须与此匹配。 The currentlineno local variable is used to mean 'the number of the next line to be read'. currentlineno局部变量用于表示“要读取的下一行的编号”。 As such it must be initialized with 1 . 因此,必须使用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. (考虑到变量的名称,这有些令人困惑。)可以将行计数器初始化为0的其他实现也是可能的-对于大多数C ++程序员而言,这确实是自然的。

4) See any textbook or online reference of C++. 4)参见C ++的任何教科书或在线参考。 Look for "pre-increment" ( ++x ) and "post-increment" ( x++ ) operators. 查找“前递增”( ++x )和“后递增”( x++ )运算符。 They have the same side effect (increment x), but differ in the value of the expression. 它们具有相同的副作用(x增量),但是表达式的值不同。 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. C ++程序员通常更喜欢预增量,因为通常可以对用户定义的类型更有效地实现它。

5) Even more basic textbook question. 5)甚至更基本的教科书问题。 a < b tests for a less-than relationship, a != b tests for inequality. a < b测试小于关系, a != b测试不平等。

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. 注意:所有答案均假定使用的类型来自标准C ++库,即<string><iostream>标头的适当包含,并using必要的using指令或声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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