解析程序:分段错误(核心转储)



我目前正在编写一个程序,该程序将读取文件/proc/stat并解析每一行,将其存储为令牌,然后最终在输出表中进行处理和表示。我正处于能够让程序解析程序的阶段,但当涉及到将令牌存储在各种数组值中时,我会得到错误:分段错误(核心转储)。我不确定是什么原因造成的,因为我已经分配了内存。我也是C.的初学者

//standard input/output file to help with io operations
#include<stdio.h>
//standard library files to help with exit and other standard functions
#include<stdlib.h>
//header file for usleep function
#include <unistd.h>
#include <string.h> //header file for strtok function
int main()
{
//FILE pointer will need to be declared initially, in this example the name is fp
FILE *fp;
//A character pointer that will store each line within the file; you will need to parse this line to extract useful information
char *str = NULL;
//size_t defined within C is a unsigned integer; you may need this for getline(..) function from stdio.h to allocate buffer dynamically
size_t len = 0;
//ssize_t is used to represent the sizes of blocks that can be read or written in a single operation through getline(..). It is similar to size_t, but must be a signed type.
ssize_t read;
float cpu_line1[4];
float cpu_line2[4];
float cpu_line3[4];
float cpu_line4[4];
float cpu_line5[4];
float page[2];
float swap[2];
float intr;
float ctxt;
float btime;

//a variable declared to keep track of the number of times we read back the file
unsigned int sample_count = 0;
//opening the file in read mode; this file must be closed after you are done through fclose(..); note that explicit location of the file to ensure file can be found
fp = fopen("/proc/stat", "r");
//checking if the file opening was successful; if not we do not want to proceed further and exit with failure code right away
if(fp == NULL)
{
    exit(EXIT_FAILURE);
}
int i = 0;
char **string = NULL; //declaration of string
string = (char**)malloc(10*sizeof(char*)); //assign space for 10 pointers to array
for (i=0; i<10; i++) //allocate 50 bytes to each string in the array
{
    string[i] = (char*)malloc(50*sizeof(char));
}
char *s = NULL;
//a loop that will read one line in the file at a time; str will read the line; len will store the length of the file
while(1)
{
    printf("e[1;1He[2J"); //this line will make sure you have cleared the previous screen using C's powerful format specifiers
    printf("----------------------------------------------------------------n");//used for presentation
    printf("Sample: %un", sample_count); //showing the sample count
    int i = 0; //counter
    while ((read = getline(&str, &len, fp)) != -1)
    {
       // printf("Retrieved line: n%sof length: %zu, allocated buffer: %u :n", str, read, (unsigned int) len);
        s = strtok(str, " ");
        printf("Test program: %sn", s);
    }
    if (i=0)
    {
        sprintf(string[0], s);
        cpu_line1[0] = atoi(strtok(NULL, " "));
        cpu_line1[1] = atoi(strtok(NULL, " "));
        cpu_line1[2] = atoi(strtok(NULL, " "));
        cpu_line1[3] = atoi(strtok(NULL, " "));
    }
    if (i=1)
    {
        sprintf(string[1], s);
        cpu_line2[0] = atoi(strtok(NULL, " "));
        cpu_line2[1] = atoi(strtok(NULL, " "));
        cpu_line2[2] = atoi(strtok(NULL, " "));
        cpu_line2[3] = atoi(strtok(NULL, " "));
    }
    if (i=2)
    {
        sprintf(string[2], s);
        cpu_line3[0] = atoi(strtok(NULL, " "));
        cpu_line3[1] = atoi(strtok(NULL, " "));
        cpu_line3[2] = atoi(strtok(NULL, " "));
        cpu_line3[3] = atoi(strtok(NULL, " "));
    }
    if (i=3)
    {
        sprintf(string[3], s);
        cpu_line4[0] = atoi(strtok(NULL, " "));
        cpu_line4[1] = atoi(strtok(NULL, " "));
        cpu_line4[2] = atoi(strtok(NULL, " "));
        cpu_line4[3] = atoi(strtok(NULL, " "));
    }
    if (i=4)
    {
        sprintf(string[4], s);
        cpu_line5[0] = atoi(strtok(NULL, " "));
        cpu_line5[1] = atoi(strtok(NULL, " "));
        cpu_line5[2] = atoi(strtok(NULL, " "));
        cpu_line5[3] = atoi(strtok(NULL, " "));
    }
    if(i=5)
    {
        sprintf(string[5], s);
        page[0] = atoi(strtok(NULL, " "));
        page[1] = atoi(strtok(NULL, " "));
    }
    if(i=6)
    {
        sprintf(string[6], s);
        swap[0] = atoi(strtok(NULL, " "));
        swap[1] = atoi(strtok(NULL, " "));
    }
    if(i=7)
    {
        sprintf(string[7], s);
        intr = atoi(strtok(NULL, " "));
    }
    if(i=8)
    {
        sprintf(string[8], s);
        ctxt = atoi(strtok(NULL, " "));
    }
    if(i=9)
    {
        sprintf(string[9], s);
        btime = atoi(strtok(NULL, " "));
    }
    printf("----------------------------------------------------------------n"); //used for presentation
    usleep(500000);//this will ensure time delay
    rewind(fp);//rewind the file pointer to start reading from the beginning
    sample_count++;//update the sample count
}
//Frees pointers to make program memory efficient
free(str);
for (i=0; i <10; i++)
{
    free(string[i]);
}
//once you are done, you should also close all file pointers to make your program memory efficient
fclose(fp);

return 0;

}

编辑以下是程序在Cygwin 中运行时的样子


样本:0

测试程序:cpu

测试程序:cpu0

测试程序:cpu1

测试程序:cpu2

测试程序:cpu3

测试程序:页面

测试程序:交换

测试程序:intr

测试程序:ctxt

测试程序:btime

分段故障(堆芯转储)

除其他外,您的条件不正确:

if (i = 1) {
    // do something.
}

这将值1分配给i,而不是与1进行比较。尝试条件为i == 1

不确定为什么分割失败,但我可以告诉你,如果你写

if (i=0) //it means assign the value 0 to i and if the value would be different from zero, follow the "then" branch
if (i==0) //is compare i to 0 and if true follow the "then" branch

所以所有的如果都有错。

希望这有帮助:)

如其他答案中所述,应将if(i=0)替换为if(i==0)

您会得到一个分段错误,因为strtok()返回NULL,因此atoi()将NULL作为参数:

    cpu_line1[0] = atoi(strtok(NULL, " "));
    cpu_line1[1] = atoi(strtok(NULL, " "));
    cpu_line1[2] = atoi(strtok(NULL, " "));
    cpu_line1[3] = atoi(strtok(NULL, " "));

只要有足够的令牌可以解析,这个代码就可以正常工作。但似乎你太频繁地试图获得下一个代币了。

在将strtok()的返回值提供给atoi()之前,您应该检查它是否为NULL。我建议使用循环来简化代码,并将NULL检查应用于atoi()的每次使用。

下面是一个简单的检查示例:

char* token = strtok(NULL, " ");
if(token != NULL)
    cpu_line1[0] = atoi(token);

如果将检查添加到每个strtok()调用中,这将解决分段错误问题。

最新更新