[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Returnerin af at "string" array



On Thu, 8 Feb 2001, |GeO| wrote:
> ok her kommer det jeg har lavet indtil videre

Ok, Jeg har "prøvet" at beholde formen på det du har lavet.
Der er nogle grimmere fejl, fx. den char** som du prøver at gemme tingene i
har du ikke afsat nogen størelse på, så du skriver bare vildt ud i
hukommelsen, dette skal man ikke være den store D. Knuth for at se er noget
af et skråplan at komme ud på.

Noget andet er at du benytter gets, denne funktion laver let buffer
overflows hvis man ikke passer på, derfor har jeg valgt at benytte getline.
Som jeg også har benyttet strncpy.

Jeg frigiver ikke de ting som jeg har allokeret, men det skyldes at jeg vil
holde koden så "simpel" som muligt, hvis du vælger at køre dette som et
program som skal køre som demon eller lign. skal dette laves om.

Jeg håber at mit exempel kan bringe dig vidre :

-Kim

-------------------------[C-kode start]--------------------------------

/*Included Libs*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <gtk/gtk.h>

#define sEND  "END";

/*Global Variables*/
int antalstop;


/*
 * The parseLog Function:
 * Returns an array of END-tag lines. ie. list of who printed what =)
 */
char** parseLog(char *filename){
  int i;
  size_t linesize;
  char *line;
  FILE *logfile;
  char **ENDLines;
  // Først finder vi antallet af stoplinier. 
  // Dette kan gøres flottere og lækre, men vi holder det simpelt
  logfile = fopen(filename, "r");
  if (!logfile){
    // Hvis noget er gået galt udskriver vi fejl beskeden og stopper her.
    perror(NULL);
    return NULL;
  }

  linesize = 0;
  line = NULL;
  antalstop = 0;
  while(getline (&line, &linesize, logfile) != EOF)
    // Hvergang der kommer et sEND, "husker" vi dette 
    if(strstr(line, sEND)) 
      antalstop++;


  fclose(logfile);

 // Hvis der ikke er nogle hits stopper vi her og retunere en NULL pointer
  if (!antalstop) {
    return NULL;
  }

  logfile = fopen(filename, "r");
  if (!logfile){
    perror(NULL);
    return NULL;
  }

  i = 0;
  ENDLines = (char**) malloc(antalstop * sizeof(char*));
  while (getline (&line, &linesize, logfile) != EOF) {
    if(strstr(line, sEND)){
      ENDLines[i] = (char*) malloc ((linesize  + 1)* sizeof(char*));
      strncpy(&ENDLines[i][0], line, linesize);
      i++;
    }
  }
  
  fclose(logfile);
  return(ENDLines);
}

int main(int argc, char *argv[]){
  int q;
  char **test;
        
  test = parseLog("lp-acct");
 
  // Og en udskrift af hvad vi har fundet.     
  for(q = 0; q < antalstop; q++)
    printf("%s",test[q]);
    
}



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:08 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *