搜索被他们不是空终止的文件中NULL结尾的字符串 [英] Searching for strings that are NULL terminated within a file where they are not NULL terminated

查看:480
本文介绍了搜索被他们不是空终止的文件中NULL结尾的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写打开两个文件的阅读计划:第一个文件包含了我店的形式名称[0] =约翰\\ 0 。第二个文件是一个包含各20名的许多OCCURENCES一个大的文本文件。

I am writing a program that opens two files for reading: the first file contains 20 names which I store in an array of the form Names[0] = John\0. The second file is a large text file that contains many occurences of each of the 20 names.

我需要我的程序来扫描每个找到的名字一次第二个文件的条款内容,并变量计数递增等的完成节目中,所有的名字出现在文本中的总数存储在计数

I need my program to scan the entirity of the second file and each time it finds one of the names, a variable Count is incremented and so on the completion of the program, the total number of all the names appearing in the text is stored in Count.

下面是我的回路,搜索和计数的名字出现的次数:

Here is my loop which searches for and counts the number of name occurences:

char LineOfText[85];
char *TempName;    

while(fgets(LineOfText, sizeof(LineOfText), fpn)){
    for(a = 0; a<NumOfNames; a++){
        TempName = strstr(LineOfText, Names[a]);
        if(TempName != NULL){
            Count++;
        }
    }
}

不管我做什么,这个循环不工作,我希望它,但我发现什么是错的(我想!)。我的问题是阵列中的每个名称为空终止,但是当一个名字出现在文本文件不是null终止,除非它的发生是由于行的最后一个字。因此,此循环仅在计数的次数任何名称出现在一行的末端,而不是任何名称的出现的次数的任意位置该文本文件。如何调整这种循环,解决这个问题?

No matter what I do, this loop doesn't work as I would expect it to, but I have discovered what is wrong (I think!). My problem is that each name in the array is NULL terminated, but when a name appears in the text file it is not NULL terminated, unless it occurs as the last word of a line. Therefore, this while loop is only counting the number of times any of the names appear at the end of a line, rather than the number of appearances of any of the names anywhere in the text file. How can I adjust this loop to combat this problem?

感谢您提出的任何建议提前。

Thank you for any advice in advance.

推荐答案

这里的问题可能是你使用与fgets ,这确实的的修剪从中读取行的换行符。

The issue here is probably your use of fgets, which does not trim the newline from the line it reads.

如果您正在创建你的名称读线,与fgets 阵列,那么所有的名字将与终止一个换行符。读取文件中的行与与fgets 也将与换行符终止,所以名称将只匹配在线条的末端。

If you are creating your names array by reading lines with fgets, then all the names will be terminated with a newline character. The lines in the file being read with fgets will also be terminated with a newline character, so the names will only match at the end of the lines.

的strstr 比不上它终止模式字符串,原因很明显的NUL字节。如果有,它只会匹配后缀字符串,这将使其成为一个非常不同的功能。

strstr does not compare the NUL byte which terminates the pattern string, for obvious reasons. If it did, it would only match suffix strings, which would make it a very different function.

此外,你只能找到一个最大的每一行每个名称的一个实例。如果你认为一个名字可能会出现不止一次在同一行,应更换:

Also, you will only find a maximum of one instance of each name in each line. If you think that a name might appear more than once in the same line, you should replace:

 TempName = strstr(LineOfText, Names[a]);
 if(TempName != NULL){
    Count++;
 }

喜欢的东西:

 for (TempName = LineOfText;
      (TempName = strstr(TempName, Names[a]);
     ++Count, ++TempName) {
 }

有关参考,在这里是与fgets 从C标准(强调)的定义是:

For reference, here is the definition of fgets from the C standard (emphasis added):

与fgets 函数读取比指定的字符数少至多一个n 从流指向通过进入阵列由指出为s 。没有额外的字符换行字符后读取(这是保留)或之后结束的文件。之后的最后一个字符读入数组中的一个空字符立即写入。

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

这是不同于,它不保留换行字符。

This is different from gets, which does not retain the new-line character.

这篇关于搜索被他们不是空终止的文件中NULL结尾的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆