我正在写一个程序,逐行读取一个文件,将单词和翻译分开。下面的代码有效。然而,我无法理解/* separate word and translation */
函数的load_dictionary
部分实际上是如何工作的。。
情况不明:p line
的输出p word
后的输出。不应该在word = line + strspn(line, DELIMS)
\t和print-ants\t之前阅读。
文件:dict.txt
WORD TRANSLATION
ants anttt
anti eti
ante soggy
anda eggs
功能:主
/* maximum number of characters for word to search */
#define WORD_MAX 256
/* maximum number of characters in line */
#ifndef LINE_MAX
#define LINE_MAX 2048
#endif
int main(int argc, char * argv[]) {
char word[WORD_MAX], * translation;
int len;
if (argc <= 1)
return 0; /* no dictionary specified */
/* load dictionary */
load_dictionary(argv[1]);
return 0;
}
函数:加载词典:-读取词典文件
/* delimiter for dictionary */
#define DELIMS "\t"
unsigned void load_dictionary(const char * filename) {
FILE * pfile;
char line[LINE_MAX], * word, * translation;
/* ensure file can be opened */
if ( !(pfile = fopen(filename,"r")) )
return icount;
/* read lines */
while ( (fgets(line, LINE_MAX, pfile)) ) {
/* strip trailing newline */
int len = strlen(line);
if (len > 0 && line[len-1] == '\n') {
line[len-1] = '\0';
--len;
}
/* separate word and translation */
word = line + strspn(line, DELIMS);
if ( !word[0] )
continue; /* no word in line */
translation = word + strcspn(word, DELIMS);
*translation++ = '\0';
translation += strspn(translation, DELIMS);
}
}
最佳答案
strspn
将给出DELIM
中出现的初始字符数strcspn
将给出DELIM
中不存在的初始字符数
(见http://man7.org/linux/man-pages/man3/strspn.3.html)
因此,代码的思想是使用简单的指针算法使word
和translation
指针指向输入中的第一个单词和输入中的第二个单词。此外,代码在第一个单词后面添加一个NUL终止符,使其看起来像两个字符串。
例子:
line: \t\t\t\tC++\0\t\t\tA programming language
^ ^ ^
| | |
| | translation points here
| |
| NUL added here
|
word points here
因此打印
word
和translation
将给出:C++
A programming language
附加注释的代码:
word = line + strspn(line, DELIMS); // Skip tabs, i.e.
// make word point to the
// first character which is
// not a tab (aka \t)
if ( !word[0] )
continue; /* no word in line */
translation = word + strcspn(word, DELIMS); // Make translation point to the
// first character after word
// which is a tab (aka \t), i.e. it
// points to the character just after
// the first word in line
*translation++ = '\0'; // Add the NUL termination and
// increment translation
translation += strspn(translation, DELIMS); // Skip tabs, i.e.
// make translation point to the
// second word in line which is
关于c - 了解strspn(const char * s,const char * accept)函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57767867/