2016-04-18 10 views
-1

Ich versuche, in einem Diagramm mit Leerzeichen getrennten Kanten zu lesen. Fehler beim Parsen vom char * -Puffer zu den INTS.Parse-Ints von Char-Array

Beispielgrafik:

1 2 
1 3 
2 3 
3 4 
4 5 
1 5 
5 6 
1 6 

Ausgang:

first edge: 1 
second edge: 2 
first edge: 0 
second edge: 0 
first edge: 1 
second edge: 3 
first edge: 0 
second edge: 0 
first edge: 2 
second edge: 3 
first edge: 0 
second edge: 0 
first edge: 3 
second edge: 4 
first edge: 0 
second edge: 0 
first edge: 4 
second edge: 5 
first edge: 0 
second edge: 0 
first edge: 1 
second edge: 5 
first edge: 0 
second edge: 0 
first edge: 5 
second edge: 6 
first edge: 0 
second edge: 0 
first edge: 0 
second edge: 0 
first edge: 1 
second edge: 6 
first edge: 0 
second edge: 0 
first edge: 0 
second edge: 0 

offensichtlich all diese Nullen nicht da sein sollte.

Code:

#include <stdio.h> 
#include <math.h> 
#include <ctype.h> 

int main(int argc, char* argv[]){ 

    FILE *fin; 
    fin = fopen(argv[1], "r"); 
    int n_e = 0; 
    char* buffer = (char*)malloc(2048); 
    char* n1 = (char*)malloc(256); 
    char* n2 = (char*)malloc(256); 
    int v_1 = 0; 
    int v_2 = 0; 
    int flag = 0; 

    while(!feof(fin)){ 
     int bytes_read = fread(buffer, 1, 2048, fin); 
     for(int i = 0; i < bytes_read; i++){ 
      if(isdigit(buffer[i])){ 
       if(flag == 0){ 
        n1[v_1++] = buffer[i]; 
       } 
       else 
        n2[v_2++] = buffer[i]; 
      } 
      else if(buffer[i] == ' ') 
       flag = 1; 
      else{ //end of the line?? 
       n_e++; 
       flag = 0; 
       n1[v_1++] = '\0'; 
       n2[v_2++] = '\0'; 
       int first = atoi(n1); 
       int second = atoi(n2); 
       printf("first edge: %d\n", first); 
       printf("second edge: %d\n", second); 
       v_1 = 0; 
       v_2 = 0; 
      } 
     } 
    } 

return 0; 
} 
+0

http://stackoverflow.com/q/5431941/3185968 – EOF

+0

Verwenden Sie 'sscanf', z.B. 'sscanf ("% d% d ", Eingabestr, & nr1, & nr2)'. siehe 'man scanf' –

+0

Gibt es einen Grund, warum Sie die Daten analysieren, um die Ganzzahlen zu extrahieren, anstatt' fscanf' oder 'sscanf' zu verwenden? –

Antwort

1

Sie können die Auswuchs erreichen, was Sie in einer einzigen Zeile wollen: die fscanf Linie. Der Rest ist der Rahmen- und Fehlerprüfung gewidmet - das ist ein wesentlicher Bestandteil jedes Programms. Angesichts Ihrer Graphkanten als Textdatei:

#include <stdio.h> 

int main(int argc, char *argv[]) { 
    FILE *fin; 
    int first, second; 
    if(argc < 2) {          // check # arguments 
     return 0;          // or other action 
    } 
    fin = fopen(argv[1], "rt"); 
    if(fin == NULL) {         // check the file opened 
     return 0;          // or other action 
    } 
    while(fscanf(fin, "%d%d", &first, &second) == 2) { // check the number of items read 
     printf("first edge: %d, second edge: %d\n", first, second); 
    } 
    fclose(fin); 
    return 0; 
} 

Programmausgabe:

first edge: 1, second edge: 2 
first edge: 1, second edge: 3 
first edge: 2, second edge: 3 
first edge: 3, second edge: 4 
first edge: 4, second edge: 5 
first edge: 1, second edge: 5 
first edge: 5, second edge: 6 
first edge: 1, second edge: 6 

Hinweis auch, dass feof nicht verwendet wird, so, wie Sie tat, wie oben ausgeführt.

0

Sie können den von der Funktion sscanf() zurückgegebenen Wert überprüfen, um festzustellen, ob die Stichprobe erfolgreich von der Linie analysiert wurde.

Ich schreiben Sie Code, um eine effizientere Art und Weise zu zeigen, Ihr Problem zu lösen:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 


#define MAX_LINE_LEN   (32) 
#define MAX_EDGES_SAMPLES  (256) 


/* Represents an Edge Object */ 
typedef struct edge_s 
{ 
    int first; 
    int second; 
} edge_t; 


int main(int argc, char * argv[]) 
{ 
    FILE * pf = NULL; 
    char line[ MAX_LINE_LEN + 1 ] = {0}; 
    int count = 0; 
    int i = 0; 

    edge_t edges[ MAX_EDGES_SAMPLES ]; /* Edges array */ 


    /* Initialize Edges array */ 
    memset(&edges, 0, sizeof(edges)); 

    /* Open input text file for reading */ 
    pf = fopen(argv[1], "r"); 

    if(!pf) 
    { 
     printf("Error openning file: %s\n", argv[1]); 
     return 1; 
    } 

    /* For each line... */ 
    while(fgets(line, MAX_LINE_LEN, pf)) 
    { 
     edge_t aux; 

     /* Parse Line */ 
     int ret = sscanf(line, "%d %d", &aux.first, &aux.second); 

     /* Ignore malformed lines */ 
     if(ret != 2) 
      continue; 

     /* Add values to the Edges array */ 
     edges[count].first = aux.first; 
     edges[count].second = aux.second; 

     /* Increment Edges Count */ 
     count++; 

     if(count >= MAX_EDGES_SAMPLES) 
      break; 
    } 

    /* Free resource */ 
    fclose(pf); 

    /* Display Edges array data */ 
    for(i = 0; i < count; i++) 
     printf("Edge[%d]: first=%d second=%d\n", i, edges[i].first, edges[i].second); 

    return 0; 
} 

/* eof */ 

hoffe, es hilft!

Verwandte Themen