Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c中标记字符串时出现seg错误_C - Fatal编程技术网

在c中标记字符串时出现seg错误

在c中标记字符串时出现seg错误,c,C,我几天前才开始用C语言编程。我的程序打开一个文件,逐行读取它,然后删除我不需要的东西(括号、换行符等),然后一旦我有了逗号分隔格式的数据,我想将其添加到数组中(然后将该数组添加到数组中)。我正在标记逗号分隔的字符串,但我一直得到EXC\u BAD\u访问,无法访问内存。当我在调试器中运行它时 我做错了什么?下面是我的代码中给出问题的部分: //now data(variable: line) looks like this: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

我几天前才开始用C语言编程。我的程序打开一个文件,逐行读取它,然后删除我不需要的东西(括号、换行符等),然后一旦我有了逗号分隔格式的数据,我想将其添加到数组中(然后将该数组添加到数组中)。我正在标记逗号分隔的字符串,但我一直得到
EXC\u BAD\u访问,无法访问内存。
当我在调试器中运行它时

我做错了什么?下面是我的代码中给出问题的部分:

//now data(variable: line) looks like this:  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4
char *str_ptr;
str_ptr = strtok(line, ",");
  for(; str_ptr != NULL ;){
    fprintf(stdout, "%s\n", str_ptr);
    str_ptr = strtok(NULL, ",");
  }
这是我的全部代码:

#include <stdio.h>

int main() {

    char line[1024];
    FILE *fp = fopen("/Users/me/Desktop/output.txt","r");

    printf("Starting.. \n");
    if( fp == NULL ) {
        return 1;
    }
    int count = 0;
    int list[30]; //items will be stored here

    while(fgets(line, 1024, fp) != EOF) {
        count++;
        //parse the text in the line Remove the open bracket, then remove the last newline,comma and close bracket
        // data looks like this: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4],
        size_t len = strlen(line);
        memmove(line, line+1, len-4);
        line[len-4] = 0;
        printf("%s \n",line);

        //parse the numbers in the char
        //now data looks like this:  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 4
        char *str_ptr;
        str_ptr = strtok(line, ",");
          for(; str_ptr != NULL ;){
            fprintf(stdout, "%s\n", str_ptr);
            str_ptr = strtok(NULL, ",");
          }


        //for testing just stop the file after two lines
        if (count == 2) {
            break;
        }

        
        if ( count > 1000000) {
            printf("count is higher than 1,000,000 \n");
            count = 0;
        }
    }
    printf(" num of lines is %i \n", count);

    return 0;
}

fgets
返回错误或EOF时的
NULL
。检查是否存在
EOF

while(fgets(line, 1024, fp) != NULL){

如果输入文件始终有效,我看不出任何错误。我试着在我的Mac上编译并运行它,它运行得非常好。也许您可以将示例文件上传到某个地方,这样我们就可以确切地看到发生了什么,因为
memmove
部分中的代码对于无效输入(例如空行或短行)来说太容易受到攻击。我建议把它改成

char* data = line;
size_t len = strlen(line);
if (len > 3) {
    line[len - 3] = '\0';
    data++;
}

char *str_ptr;
str_ptr = strtok(data, ",");
for(; str_ptr != NULL ;){
    fprintf(stdout, "%s\n", str_ptr);
    str_ptr = strtok(NULL, ",");
}

我的代码离完美的数据验证还差得很远,但至少它不会在空行上抛出seg错误。

我仍然遇到同样的问题。如果我在尝试标记字符时注释掉了部分,那么程序就完成了,但是当我添加它时,它就提前结束了;谢谢你,瑟斯特马斯特。除了你上面写的,你还有什么改变吗?我读了我的代码和你的代码,看起来都是一样的,但我却发现了这些错误@wildplasser我不确定这是否重要,因为我想添加到列表中的项目数始终是相同的(30)@wildplasser:是的,当fgets达到EOF时可能会发生这种情况,但由于恶劣的while条件,它会继续@错误404:不,我做的唯一其他更改是使用stdin,并注释掉
返回1你在哪一行得到了SEGFULT?@interjay我没有意识到我多次收到这个错误。我从调试器发布了堆栈。这有帮助吗?或者我能得到其他信息吗?
char* data = line;
size_t len = strlen(line);
if (len > 3) {
    line[len - 3] = '\0';
    data++;
}

char *str_ptr;
str_ptr = strtok(data, ",");
for(; str_ptr != NULL ;){
    fprintf(stdout, "%s\n", str_ptr);
    str_ptr = strtok(NULL, ",");
}