简体   繁体   English

strstr C 函数运行异常

[英]strstr C function is functioning abnormally

For the C project coming up, the goal is to read in a CSV file with the first two lines listing the row and column lengths like对于即将推出的 C 项目,目标是读取一个 CSV 文件,其中前两行列出了行和列的长度,例如

attributes: 23
lines: 1000
e,x,y,n,t,l,f,c,b,p,e,r,s,y,w,w,p,w,o,p,n,y,p
e,b,y,y,t,l,f,c,b,n,e,c,s,s,w,w,p,w,o,p,n,s,m
e,x,f,y,t,l,f,w,n,w,t,b,s,s,w,w,p,w,o,p,n,v,d
e,s,f,g,f,n,f,c,n,k,e,e,s,s,w,w,p,w,o,p,k,v,u

The thing is, I don't know if future file inputs will be of the same row/column lengths, so I'm implementing a determineFormat function to read those first two lines, which will be used for building the data structures.问题是,我不知道未来的文件输入是否具有相同的行/列长度,所以我正在实现一个determineFormat格式函数来读取前两行,这将用于构建数据结构。

In order to do this, I need to match a substring to the current line.为了做到这一点,我需要将一个子字符串与当前行进行匹配。 If it matches, then fscanf is used to read in the line and extract the length integers.如果匹配,则使用fscanf读取该行并提取长度整数。 However, this code isn't working, as the entire strstr function is getting skipped over in ddd.但是,此代码不起作用,因为整个strstr函数在 ddd 中都被跳过了。

int lineCount, attrCount; //global variables

void determineFormats(FILE *incoming){

    char *curLine= emalloc(CLINPUT);
    int i;
    char *ptr=NULL;

    for (i=0; i<2; i++){
        if (fgets(curLine, CLINPUT, incoming) != NULL){
            ptr= strstr(curLine, "attrib");  //this line is skipped over

            if (ptr!= NULL)
                fscanf(incoming, "attributes: %d", &attrCount);

            else 
                fscanf(incoming, "lines: %d", &lineCount);  

        }
    }

    printf("Attribute Count for the input file is: %d\n", attrCount);
    printf("Line count is: %d\n", lineCount);

}

My thinking for the if/else block is since there are only two lines of interest to this function, and they're both at the head of the file, just scan each line and test if the string matches.我对 if/else 块的想法是因为这个函数只有两行感兴趣,而且它们都在文件的开头,只需扫描每一行并测试字符串是否匹配。 If it does, then the non-null conditional is run, otherwise the other conditional is executed.如果是,则运行非空条件,否则执行另一个条件。 However, in this case, the strstr function is getting skipped.但是,在这种情况下,会跳过strstr函数。

Extra Info额外信息

Some of the comments made me go back and double check.一些评论让我回去仔细检查。

CLINPUT is defined to be 100, or roughly 40% again the number of characters to read from each line. CLINPUT 被定义为 100,或大约 40% 再次从每行读取的字符数。

This is the output from ddd when ptr= strstr(curLine, "attrib");这是当ptr= strstr(curLine, "attrib");时 ddd 的输出ptr= strstr(curLine, "attrib"); is called:叫做:

0xb7eeaff0 in strstr () from /lib/libc.so.6
Single stepping until exit from function strstr,
which has no line number information.

Once this happens, the line indicator disappears, and single stepping (F5) from that point returns to the calling function.一旦发生这种情况,线条指示器就会消失,并且从该点单步执行 (F5) 返回到调用函数。

strstr is working good. strstr 运行良好。 Problem is that fscanf will read next line since current already read.问题是 fscanf 将读取下一行,因为当前已读取。

Here's more correct way这里有更正确的方法

for (i=0; i<2; i++){
    if (fgets(curLine, CLINPUT, incoming) != NULL){
        if (strstr(curLine, "attributes:")) {
            sscanf(curLine, "attributes: %d", &attrCount);
        } else if (strstr(curLine, "lines:")) {
            sscanf(curLine, "lines: %d", &lineCount);  
        }

    }
}

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

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