2016-06-11 6 views
-1

は、私はそれに保存されたラインの未知の長さ、で、.txtファイルを取得する必要があり、リストにC、動的な文字列

typedef struct LISTA lista; 
struct LISTA 
{ 
    char *linia; 
    int numer; 
    struct lista *next; 
}; 

広告を持っています。 "linia"は動的に行番号malloc() -ed、行番号は "numer"です。

私はそれをcharでcharを読み込むように動かすようにしましたが、Cの限られた知識で私は非常に素早くそれに巻き込まれました。私はここでいくつかの解決策を見つけましたが、それらの大部分は1つまたは別の問題を抱えていました。いくつかは非常に複雑で、何ができたのか理解できず、文字列の最後に '\ 0'

私は実際にどのようにしてどのように例と説明ができてうれしいですか。私は実際にこの愚かな課題を終わらせるだけでなく、それを学びたいと思っています

実際の割り当てはCでgrepのようなプログラムを作っていますが、私は残りのメカニックを手に入れることができると思います。動的リスト

+2

これはコーディングサービスではありません。大文字の大文字の名前は、マクロとenum-constantsにのみ使用するようにしてください。また、Cは大文字と小文字を区別します。 'typedef'宣言と' struct'宣言を使ってパターンをコピーするだけであれば、その概念を知る必要があります。さもなければ、あなたはプログラミングのどこにも行きません。 – Olaf

答えて

0

コードを参照してください。テキストファイルを動的リスト、動的文字列に読み込みます。ここでは char * getLineOfAnySize(FILE* fp, size_t typicalSize, int *endOfLineDetected);が重要です。

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

struct list 
{ 
    char *linia; 
    int numer; 
    struct list *next; 
}; 

typedef struct list LIST; 

char * getLineOfAnySize(FILE* fp, size_t typicalSize, int *endOfLineDetected,size_t *nrOfCharRead){ 
    char *line;  // buffer for our string 
    int ch;   // we will read line character by character 
    size_t len = 0; // number of characters read (character counter) 
    size_t lineSize = typicalSize; // initial size of the buffer allocated for the line 
    *nrOfCharRead = 0; 

    if(!fp) return NULL; // protection 

    // allocating the buffer 
    line = realloc(NULL, sizeof(char)*lineSize); // expected size of the line is up to typicalSize 

    if (!line) return line; // protection, if we fail to allocate the memory we will return NULL 

    while (1) { // loop forever  
     ch = fgetc(fp);  // getting character by character from file 

     if (ch == '\n') break; // end of line detected - breaking the loop 
     if(ch == EOF) { 
      *endOfLineDetected = 1; 
      break; // end of file detected - breaking the loop 
     } 

     line[len++] = ch;  // store the character in the line buffer, increase character counter 

     if (len == lineSize){ // we reached the end of line buffer (no more room) 

     lineSize = lineSize + 64; // we have to increase the line size 
     line = realloc(line, sizeof(char)*(lineSize)); // line buffer has new size now 

     if (!line) return line; // if we fail to allocate memory we will return NULL 
     } 
    } 

    if((len == 0) && *endOfLineDetected) return NULL; // empty file 

    line[len++] ='\0'; // ending the string (notice there is no '\n' in the string) 
    *nrOfCharRead = len; 

    return line;  // return the string 
} 

int main(void) 
{ 
    FILE *fp = NULL; // file handle 
    char *line; // 
    int endOfLineDetected = 0; 
    size_t nrOfCharRead = 0; 

    LIST *current, *head; // pointers to list elements 

    head = current = NULL; // init to NULL 

    fp = fopen("document.txt", "r"); // open file for reading 
    int nr = 0; 

    while(line = getLineOfAnySize(fp,128,&endOfLineDetected,&nrOfCharRead)){ // read the file 

     if((nrOfCharRead == 0) && endOfLineDetected) break;    

     // create new list element 

     LIST *node = malloc (sizeof(LIST)); 

     nr = nr + 1; 
     node->linia = line; // initialize the linia 
     node->numer = nr;  // update the line number 

     node->next = NULL; // next element do not exist yet 

     if(head == NULL) 
     { 
      current = head = node; 
     } else 
     { 
      current = current->next = node; 
     } 

     if (endOfLineDetected) break; 
    } 

    if (fp) fclose(fp); // remember to close the file 

    //print, go via all elements of the list till you get NULL next element 
    for(current = head; current ; current=current->next){ 
     printf("line nr=%d line= %s",current->numer, current->linia); 
    } 
    return 0; 
} 
+0

ありがとう!それは私が期待していたほどです。これはまさに私が必要としていたものです。私は自分のバージョンを行うことができるはずです。 – markoatonc