2015-12-17 10 views
5

Ich schreibe gerade ein Programm, das die Datei/proc/stat liest und jede Zeile analysiert, als Token gespeichert und dann schließlich verarbeitet und in einer Ausgabetabelle dargestellt wird. Ich bin in der Phase, in der ich das Programm zum Parsen des Programms bekommen konnte, aber wenn es darum geht, die Token in verschiedenen Array-Werten zu speichern, bekomme ich den Fehler: Segmentierungsfehler (Core Dumped). Ich bin mir nicht sicher, was das verursacht, da ich bereits Speicher zugewiesen habe. Ich bin auch ziemlich ein Anfänger mit C.Parsing-Programm: Segmentierungsfehler (Core Dumped)

//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;1H\e[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: %u\n", 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: %s\n", 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; 

}

EDIT Hier wird eine Kopie von dem, was das Programm aussieht, wenn


Probe in Cygwin laufen: 0

Testprogramm: cpu

Testprogramm: cpu0

Testprogramm: cpu1

Testprogramm: cpu2

Testprogramm: CPU3

Testprogramm: Seite

Testprogramm: swap

Testprogramm: intr

Testprogramm: ctxt

Testprogramm: btime

Fehler Segmentation (core dumped)

+3

'gdb' ............ –

+0

Was versuchen Sie mit den 'sprintf'-Anweisungen? Wenn Sie versuchen, den Inhalt von 's' nach' string [i] 'zu kopieren, sollten Sie die Funktion' strcpy' verwenden. In 'sprintf' ist das zweite Argument eine Format-Zeichenkette, und je nachdem, was da drin ist, können böse Dinge passieren. –

+0

Compiler Warnungen aktivieren ('='!). –

Antwort

1

Wie in den anderen Anwers erwähnt sollten Sie if(i=0) mit if(i==0) ersetzen.

Sie erhalten eine Segmentierungsfehler weil strtok() NULL und somit atoi zurückgibt() wird NULL als Parameter:

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

Dieser Code ok so lange funktioniert, wie es genug Token zu analysieren sind. Aber wie es scheint, versuchst du zu oft das nächste Token zu bekommen.

Sie sollten überprüfen, ob der Rückgabewert von strtok() NULL ist, bevor Sie es an atoi() übergeben. Ich schlage vor, Schleifen zu verwenden, um Ihren Code zu vereinfachen und die Überprüfung für NULL auf jede Verwendung von atoi() anzuwenden.

Hier ist ein einfaches Beispiel für die Prüfung:

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

Dies sollte Ihren Segmentierungsfehler Problem lösen, wenn Sie die Prüfung zu jedem strtok() -Aufruf hinzufügen.

+0

muchos gracias mein Freund –

3

Unter anderem sind Ihre Bedingungen falsch:

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

Dies weist den Wert 1-i statt Vergleich zu 1 . Versuchen Sie den Zustand unter i == 1.

+0

Danke. Ich habe das geändert, aber ich bekomme immer noch die gleiche Nachricht. Hier ist eine Kopie der Ausgabe, wenn sie in Cygwin ausgeführt wird --------------------------------------- ------------------------- Probe: 0 Testprogramm: cpu Testprogramm: cpu0 Testprogramm: cpu1 Testprogramm: cpu2 Testprogramm: CPU3 Testprogramm: Seite Testprogramm: swap Testprogramm: intr Testprogramm: ctxt Testprogramm: btime Segmentation fault (core dumped) –

3

nicht sicher, warum Fehler Segmentation, aber ich kann Ihnen sagen, dass, wenn Sie

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 

Also alles schreiben, wenn es falsch sind.

this helps :)

+0

Dank. Ich habe das versucht, aber ich bekomme immer noch den Fehler, aber die Hilfe wird geschätzt –

Verwandte Themen